Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting segmentation fault when calling function #39763

Closed
ForceBru opened this issue Feb 20, 2021 · 5 comments
Closed

Getting segmentation fault when calling function #39763

ForceBru opened this issue Feb 20, 2021 · 5 comments
Labels
bug Indicates an unexpected problem or unintended behavior compiler:codegen Generation of LLVM IR and native code

Comments

@ForceBru
Copy link

ForceBru commented Feb 20, 2021

I have this code:

import InteractiveUtils
using LinearAlgebra, Printf
import ForwardDiff

InteractiveUtils.versioninfo()

function phi(x, a, sigma_sq)
	exp.(.- (x .- a) .^ 2 ./ (2 .* sigma_sq)) ./ sqrt.(2 .* pi .* sigma_sq)
end

function log_lik(p, x, a, sigma_sq)
	sum(log.(p' * phi(x', a, sigma_sq)))
end

function loss(params, x, a, sigma_sq)
	lmb = params[1]
	p = params[2:end]
	-log_lik(p, x, a, sigma_sq) + lmb * (sum(p) - 1) + sum(log, p)
end

import DelimitedFiles

dPrice = [0.005944018752161953, 0.009004873765502658, 0.01746635486195954, -0.00823552008642907, 0.013688426660577326, -0.006972894706889332, -0.0045738759415817395, 0.010942358420944227, 0.0018121418429264333, 0.001507613733466412, -0.0012059091000116847, 0.0009045681304269146, 0.001204819422850336, -0.0030147746680899068, -0.002418380864281527, 0.007237667300230603, -0.0021055805643526763, 0.003906841933992776, -0.0036057731374992967, -0.0027128878733442634, 0.0027128878733441975, -0.0024110921867319226, 0.00030170463345466526, -0.0009053871148125678, -0.00181323712418072, 0.0027186242389932425, 0.0015071592905715604, -0.0015071592905714739, -0.0018115946983506027, 0.0003021604494963292]

sigma_sq = range(0.001, 0.03; length=10) .^ 2

params0 = [1; 1/10; 1/10; 1/10; 1/10; 1/10; 1/10; 1/10; 1/10; 1/10; 1/10]

function newton(loss, params0::Vector{T}, x::Vector{T}, a::T, sigma_sq::Vector{T}, tol=1e-3; max_iter::UInt=UInt(100), lr=0.01) where T
	println("In the function")
	@assert max_iter > 0

	println("Doesn't ever get here")
	
	n_params, = size(params0)
	params = copy(params0)
	
	loss_one_arg = params -> loss(params, x, a, sigma_sq)
	
	grad = Vector{T}(undef, n_params)
	hess = Matrix{T}(undef, n_params, n_params)
	
	for i in 1:max_iter	
		Δparams = - inv(hess) * grad
		
		# Find lambda that doesn't let params escape
		use_lmb = missing
		for lmb_ in 1:-0.001:0.001
			if sum(params .+ lmb_ .* Δparams .> 0) == n_params
				use_lmb = lmb_
				break
			end
		end
		
		if ismissing(use_lmb)
			error("Could not find lambda")
		end
		
		params .+= use_lmb .* Δparams
	end
	
	params, loss_old
end

println("OK before call: ", typeof(params0), typeof(dPrice), typeof(sigma_sq))
params, loss_val = newton(
	loss, params0, dPrice, 0.0, sigma_sq
)

And this Project.toml:

[deps]
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"

Running with Julia 1.6:

forcebru ~/t/bug2> julia-1.6 --project=env1.6/ julia_bug2.jl                                                                (base) 
Julia Version 1.6.0-rc1
Commit a58bdd9010 (2021-02-06 15:49 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin19.5.0)
  CPU: Intel(R) Core(TM) i5-3330S CPU @ 2.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, ivybridge)
OK before call: Vector{Float64}Vector{Float64}Vector{Float64}
Assertion failed: (castIsValid(op, S, Ty) && "Invalid cast!"), function Create, file /workspace/srcdir/llvm-project/llvm/lib/IR/Instructions.cpp, line 2873.

signal (6): Abort trap: 6
in expression starting at /Users/forcebru/test/bug2/julia_bug2.jl:66
__pthread_kill at /usr/lib/system/libsystem_kernel.dylib (unknown line)
Allocations: 9850476 (Pool: 9847486; Big: 2990); GC: 9
fish: Job 1, '/Users/forcebru/Desktop/Julia/J…' terminated by signal SIGABRT (Abort)
forcebru ~/t/bug2 [SIGABRT]>

With latest nightly:

forcebru ~/t/bug2> julia-1.7 --project=env/ julia_bug2.jl                                                                   (base) 
Julia Version 1.7.0-DEV.578
Commit 67b9d4db2f (2021-02-19 11:46 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin18.7.0)
  CPU: Intel(R) Core(TM) i5-3330S CPU @ 2.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, ivybridge)
OK before call: Vector{Float64}Vector{Float64}Vector{Float64}

signal (11): Segmentation fault: 11
in expression starting at /Users/forcebru/test/bug2/julia_bug2.jl:66
_ZN4llvm11PointerType3getEPNS_4TypeEj at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZN4llvm12InstCombiner18visitAddrSpaceCastERNS_17AddrSpaceCastInstE at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZN4llvm12InstCombiner3runEv at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZL31combineInstructionsOverFunctionRN4llvm8FunctionERNS_19InstCombineWorklistEPNS_9AAResultsERNS_15AssumptionCacheERNS_17TargetLibraryInfoERNS_13DominatorTreeERNS_25OptimizationRemarkEmitterEPNS_18BlockFrequencyInfoEPNS_18ProfileSummaryInfoEjPNS_8LoopInfoE at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZN4llvm24InstructionCombiningPass13runOnFunctionERNS_8FunctionE at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZN4llvm13FPPassManager13runOnFunctionERNS_8FunctionE at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZN4llvm13FPPassManager11runOnModuleERNS_6ModuleE at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZN4llvm6legacy15PassManagerImpl3runERNS_6ModuleE at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZN9JuliaOJIT9CompilerTclERN4llvm6ModuleE at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
_ZN4llvm3orc14IRCompileLayer4emitENS0_29MaterializationResponsibilityENS0_16ThreadSafeModuleE at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZN4llvm3orc31BasicIRLayerMaterializationUnit11materializeENS0_29MaterializationResponsibilityE at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZN4llvm3orc16ExecutionSession26materializeOnCurrentThreadENSt3__110unique_ptrINS0_19MaterializationUnitENS2_14default_deleteIS4_EEEENS0_29MaterializationResponsibilityE at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZNSt3__18__invokeIRPFvNS_10unique_ptrIN4llvm3orc19MaterializationUnitENS_14default_deleteIS4_EEEENS3_29MaterializationResponsibilityEEJS7_S8_EEEDTclclsr3std3__1E7forwardIT_Efp_Espclsr3std3__1E7forwardIT0_Efp0_EEEOSC_DpOSD_ at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZN4llvm3orc16ExecutionSession23dispatchMaterializationENSt3__110unique_ptrINS0_19MaterializationUnitENS2_14default_deleteIS4_EEEENS0_29MaterializationResponsibilityE at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZN4llvm3orc16ExecutionSession17runOutstandingMUsEv at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZN4llvm3orc16ExecutionSession6lookupENS0_10LookupKindERKNSt3__16vectorINS3_4pairIPNS0_8JITDylibENS0_19JITDylibLookupFlagsEEENS3_9allocatorIS9_EEEENS0_15SymbolLookupSetENS0_11SymbolStateENS_15unique_functionIFvNS_8ExpectedINS_8DenseMapINS0_15SymbolStringPtrENS_18JITEvaluatedSymbolENS_12DenseMapInfoISK_EENS_6detail12DenseMapPairISK_SL_EEEEEEEEENS3_8functionIFvRKNSJ_IS7_NS_8DenseSetISK_SN_EENSM_IS7_EENSP_IS7_SX_EEEEEEE at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZN4llvm3orc16ExecutionSession6lookupERKNSt3__16vectorINS2_4pairIPNS0_8JITDylibENS0_19JITDylibLookupFlagsEEENS2_9allocatorIS8_EEEERKNS0_15SymbolLookupSetENS0_10LookupKindENS0_11SymbolStateENS2_8functionIFvRKNS_8DenseMapIS6_NS_8DenseSetINS0_15SymbolStringPtrENS_12DenseMapInfoISM_EEEENSN_IS6_EENS_6detail12DenseMapPairIS6_SP_EEEEEEE at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZN4llvm3orc16ExecutionSession6lookupERKNSt3__16vectorINS2_4pairIPNS0_8JITDylibENS0_19JITDylibLookupFlagsEEENS2_9allocatorIS8_EEEENS0_15SymbolStringPtrENS0_11SymbolStateE at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZN4llvm3orc16ExecutionSession6lookupENS_8ArrayRefIPNS0_8JITDylibEEENS0_15SymbolStringPtrENS0_11SymbolStateE at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZN4llvm3orc16ExecutionSession6lookupENS_8ArrayRefIPNS0_8JITDylibEEENS_9StringRefENS0_11SymbolStateE at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libLLVM.dylib (unknown line)
_ZN9JuliaOJIT9addModuleENSt3__110unique_ptrIN4llvm6ModuleENS0_14default_deleteIS3_EEEE at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
_ZL12jl_add_to_eeRNSt3__110unique_ptrIN4llvm6ModuleENS_14default_deleteIS2_EEEERNS1_9StringMapIPS5_NS1_15MallocAllocatorEEERNS1_8DenseMapIPS2_iNS1_12DenseMapInfoISD_EENS1_6detail12DenseMapPairISD_iEEEERNS_6vectorINSL_IS8_NS_9allocatorIS8_EEEENSM_ISO_EEEEi at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
_ZL20_jl_compile_codeinstP19_jl_code_instance_tP15_jl_code_info_tm at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
jl_generate_fptr at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
jl_compile_method_internal at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
jl_apply_generic at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
newton at /Users/forcebru/test/bug2/julia_bug2.jl:30
newton at /Users/forcebru/test/bug2/julia_bug2.jl:30
unknown function (ip: 0x1131e37f7)
jl_apply_generic at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
do_call at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
eval_body at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
jl_interpret_toplevel_thunk at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
jl_toplevel_eval_flex at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
jl_toplevel_eval_flex at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
jl_toplevel_eval_in at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
eval at ./boot.jl:369 [inlined]
include_string at ./loading.jl:1094
jl_apply_generic at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
_include at ./loading.jl:1148
include at ./Base.jl:386
jl_apply_generic at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
exec_options at ./client.jl:292
_start at ./client.jl:492
jfptr__start_36213.clone_1 at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/sys.dylib (unknown line)
jl_apply_generic at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
true_main at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
repl_entrypoint at /Users/forcebru/Desktop/Julia/Julia-1.7.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.dylib (unknown line)
Allocations: 9089757 (Pool: 9087291; Big: 2466); GC: 9
fish: Job 1, '/Users/forcebru/Desktop/Julia/J…' terminated by signal SIGSEGV (Address boundary error)
forcebru ~/t/bug2 [SIGSEGV]>

The last error is saying the error is at newton at /Users/forcebru/test/bug2/julia_bug2.jl:30, but it doesn't matter what I put in this line - the error stays the same. It looks like the function doesn't even start executing.

@vchuravy vchuravy added bug Indicates an unexpected problem or unintended behavior rr trace wanted An rr trace would help with debugging this issue - you can help out by creating one labels Feb 20, 2021
@vtjnash
Copy link
Member

vtjnash commented Feb 20, 2021

With assertions enabled, this is the same as #39641 (ProximalAlgorithms)

@vtjnash vtjnash added compiler:codegen Generation of LLVM IR and native code and removed rr trace wanted An rr trace would help with debugging this issue - you can help out by creating one labels Feb 20, 2021
@ForceBru
Copy link
Author

With assertions enabled, this is the same as #39641 (ProximalAlgorithms)

What can I do to solve or circumvent this as a user? I want to run my code.

@ForceBru
Copy link
Author

I "fixed" it by removing the use of missing:

use_lmb = -1 # missing
for lmb_ in 1:-0.001:0.001
    if sum(params .+ lmb_ .* Δparams .> 0) == n_params
        use_lmb = lmb_
        break
    end
end
		
if use_lmb == -1 # ismissing(use_lmb)
    error("Could not find lambda")
end

@vtjnash
Copy link
Member

vtjnash commented Feb 20, 2021

You could probably use @isdefined use_lmb instead. This happens with type-unstable code in some circumstances, but I'm hoping to get a fix for it out soon.

@vtjnash
Copy link
Member

vtjnash commented Mar 15, 2021

Fixed by #39747

@vtjnash vtjnash closed this as completed Mar 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior compiler:codegen Generation of LLVM IR and native code
Projects
None yet
Development

No branches or pull requests

3 participants