Created
July 15, 2018 03:55
-
-
Save dan-zheng/687509a60296e4d9c1f9da8ac61fbc31 to your computer and use it in GitHub Desktop.
Check if AD works with parameter update SIL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Model { | |
var w: Float | |
struct Parameters { | |
var w: Float | |
} | |
var allParameters: Parameters { | |
get { | |
return Parameters(w: w) | |
} | |
set { | |
w = newValue.w | |
} | |
} | |
mutating func updateParameters(gradients: Parameters, predicate: (inout Float, Float) -> Void) { | |
predicate(&w, gradients.w) | |
} | |
} | |
func optimize(parameters: inout Model.Parameters, gradients: Model.Parameters) { | |
// #forZippedParameters(parameters, gradients) { param, gradient in | |
// param -= 0.1 * gradient | |
// } | |
parameters.w -= 0.1 * gradients.w | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ swiftc -emit-silgen param.swift | |
sil_stage raw | |
import Builtin | |
import Swift | |
import SwiftShims | |
struct Model { | |
@sil_stored var w: Float { get set } | |
struct Parameters { | |
@sil_stored var w: Float { get set } | |
init(w: Float) | |
} | |
var allParameters: Model.Parameters { get set } | |
mutating func updateParameters(gradients: Model.Parameters, predicate: (inout Float, Float) -> ()) | |
init(w: Float) | |
} | |
func optimize(parameters: inout Model.Parameters, gradients: Model.Parameters) | |
// main | |
sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 { | |
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>): | |
%2 = integer_literal $Builtin.Int32, 0 // user: %3 | |
%3 = struct $Int32 (%2 : $Builtin.Int32) // user: %4 | |
return %3 : $Int32 // id: %4 | |
} // end sil function 'main' | |
// Model.w.getter | |
sil hidden [transparent] @$S5param5ModelV1wSfvg : $@convention(method) (Model) -> Float { | |
// %0 // users: %2, %1 | |
bb0(%0 : $Model): | |
debug_value %0 : $Model, let, name "self", argno 1 // id: %1 | |
%2 = struct_extract %0 : $Model, #Model.w // user: %3 | |
return %2 : $Float // id: %3 | |
} // end sil function '$S5param5ModelV1wSfvg' | |
// Model.w.setter | |
sil hidden [transparent] @$S5param5ModelV1wSfvs : $@convention(method) (Float, @inout Model) -> () { | |
// %0 // users: %6, %2 | |
// %1 // users: %4, %3 | |
bb0(%0 : $Float, %1 : $*Model): | |
debug_value %0 : $Float, let, name "value", argno 1 // id: %2 | |
debug_value_addr %1 : $*Model, var, name "self", argno 2 // id: %3 | |
%4 = begin_access [modify] [unknown] %1 : $*Model // users: %7, %5 | |
%5 = struct_element_addr %4 : $*Model, #Model.w // user: %6 | |
assign %0 to %5 : $*Float // id: %6 | |
end_access %4 : $*Model // id: %7 | |
%8 = tuple () // user: %9 | |
return %8 : $() // id: %9 | |
} // end sil function '$S5param5ModelV1wSfvs' | |
// Model.w.materializeForSet | |
sil hidden [transparent] @$S5param5ModelV1wSfvm : $@convention(method) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout Model) -> (Builtin.RawPointer, Optional<Builtin.RawPointer>) { | |
// %2 // user: %3 | |
bb0(%0 : $Builtin.RawPointer, %1 : $*Builtin.UnsafeValueBuffer, %2 : $*Model): | |
%3 = struct_element_addr %2 : $*Model, #Model.w // user: %4 | |
%4 = address_to_pointer %3 : $*Float to $Builtin.RawPointer // user: %6 | |
%5 = enum $Optional<Builtin.RawPointer>, #Optional.none!enumelt // user: %6 | |
%6 = tuple (%4 : $Builtin.RawPointer, %5 : $Optional<Builtin.RawPointer>) // user: %7 | |
return %6 : $(Builtin.RawPointer, Optional<Builtin.RawPointer>) // id: %7 | |
} // end sil function '$S5param5ModelV1wSfvm' | |
// Model.Parameters.w.getter | |
sil hidden [transparent] @$S5param5ModelV10ParametersV1wSfvg : $@convention(method) (Model.Parameters) -> Float { | |
// %0 // users: %2, %1 | |
bb0(%0 : $Model.Parameters): | |
debug_value %0 : $Model.Parameters, let, name "self", argno 1 // id: %1 | |
%2 = struct_extract %0 : $Model.Parameters, #Model.Parameters.w // user: %3 | |
return %2 : $Float // id: %3 | |
} // end sil function '$S5param5ModelV10ParametersV1wSfvg' | |
// Model.Parameters.w.setter | |
sil hidden [transparent] @$S5param5ModelV10ParametersV1wSfvs : $@convention(method) (Float, @inout Model.Parameters) -> () { | |
// %0 // users: %6, %2 | |
// %1 // users: %4, %3 | |
bb0(%0 : $Float, %1 : $*Model.Parameters): | |
debug_value %0 : $Float, let, name "value", argno 1 // id: %2 | |
debug_value_addr %1 : $*Model.Parameters, var, name "self", argno 2 // id: %3 | |
%4 = begin_access [modify] [unknown] %1 : $*Model.Parameters // users: %7, %5 | |
%5 = struct_element_addr %4 : $*Model.Parameters, #Model.Parameters.w // user: %6 | |
assign %0 to %5 : $*Float // id: %6 | |
end_access %4 : $*Model.Parameters // id: %7 | |
%8 = tuple () // user: %9 | |
return %8 : $() // id: %9 | |
} // end sil function '$S5param5ModelV10ParametersV1wSfvs' | |
// Model.Parameters.w.materializeForSet | |
sil hidden [transparent] @$S5param5ModelV10ParametersV1wSfvm : $@convention(method) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout Model.Parameters) -> (Builtin.RawPointer, Optional<Builtin.RawPointer>) { | |
// %2 // user: %3 | |
bb0(%0 : $Builtin.RawPointer, %1 : $*Builtin.UnsafeValueBuffer, %2 : $*Model.Parameters): | |
%3 = struct_element_addr %2 : $*Model.Parameters, #Model.Parameters.w // user: %4 | |
%4 = address_to_pointer %3 : $*Float to $Builtin.RawPointer // user: %6 | |
%5 = enum $Optional<Builtin.RawPointer>, #Optional.none!enumelt // user: %6 | |
%6 = tuple (%4 : $Builtin.RawPointer, %5 : $Optional<Builtin.RawPointer>) // user: %7 | |
return %6 : $(Builtin.RawPointer, Optional<Builtin.RawPointer>) // id: %7 | |
} // end sil function '$S5param5ModelV10ParametersV1wSfvm' | |
// Model.Parameters.init(w:) | |
sil hidden @$S5param5ModelV10ParametersV1wAESf_tcfC : $@convention(method) (Float, @thin Model.Parameters.Type) -> Model.Parameters { | |
// %0 // user: %2 | |
bb0(%0 : $Float, %1 : $@thin Model.Parameters.Type): | |
%2 = struct $Model.Parameters (%0 : $Float) // user: %3 | |
return %2 : $Model.Parameters // id: %3 | |
} // end sil function '$S5param5ModelV10ParametersV1wAESf_tcfC' | |
// Model.allParameters.getter | |
sil hidden @$S5param5ModelV13allParametersAC0D0Vvg : $@convention(method) (Model) -> Model.Parameters { | |
// %0 // users: %3, %1 | |
bb0(%0 : $Model): | |
debug_value %0 : $Model, let, name "self", argno 1 // id: %1 | |
%2 = metatype $@thin Model.Parameters.Type // user: %5 | |
%3 = struct_extract %0 : $Model, #Model.w // user: %5 | |
// function_ref Model.Parameters.init(w:) | |
%4 = function_ref @$S5param5ModelV10ParametersV1wAESf_tcfC : $@convention(method) (Float, @thin Model.Parameters.Type) -> Model.Parameters // user: %5 | |
%5 = apply %4(%3, %2) : $@convention(method) (Float, @thin Model.Parameters.Type) -> Model.Parameters // user: %6 | |
return %5 : $Model.Parameters // id: %6 | |
} // end sil function '$S5param5ModelV13allParametersAC0D0Vvg' | |
// Model.allParameters.setter | |
sil hidden @$S5param5ModelV13allParametersAC0D0Vvs : $@convention(method) (Model.Parameters, @inout Model) -> () { | |
// %0 // users: %4, %2 | |
// %1 // users: %5, %3 | |
bb0(%0 : $Model.Parameters, %1 : $*Model): | |
debug_value %0 : $Model.Parameters, let, name "newValue", argno 1 // id: %2 | |
debug_value_addr %1 : $*Model, var, name "self", argno 2 // id: %3 | |
%4 = struct_extract %0 : $Model.Parameters, #Model.Parameters.w // user: %7 | |
%5 = begin_access [modify] [unknown] %1 : $*Model // users: %8, %6 | |
%6 = struct_element_addr %5 : $*Model, #Model.w // user: %7 | |
assign %4 to %6 : $*Float // id: %7 | |
end_access %5 : $*Model // id: %8 | |
%9 = tuple () // user: %10 | |
return %9 : $() // id: %10 | |
} // end sil function '$S5param5ModelV13allParametersAC0D0Vvs' | |
// closure #1 in Model.allParameters.materializeForSet | |
sil private [transparent] @$S5param5ModelV13allParametersAC0D0VvmytfU_ : $@convention(method) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout Model, @thick Model.Type) -> () { | |
// %0 // user: %4 | |
// %2 // user: %7 | |
bb0(%0 : $Builtin.RawPointer, %1 : $*Builtin.UnsafeValueBuffer, %2 : $*Model, %3 : $@thick Model.Type): | |
%4 = pointer_to_address %0 : $Builtin.RawPointer to [strict] $*Model.Parameters // user: %5 | |
%5 = load [trivial] %4 : $*Model.Parameters // user: %7 | |
// function_ref Model.allParameters.setter | |
%6 = function_ref @$S5param5ModelV13allParametersAC0D0Vvs : $@convention(method) (Model.Parameters, @inout Model) -> () // user: %7 | |
%7 = apply %6(%5, %2) : $@convention(method) (Model.Parameters, @inout Model) -> () | |
%8 = tuple () // user: %9 | |
return %8 : $() // id: %9 | |
} // end sil function '$S5param5ModelV13allParametersAC0D0VvmytfU_' | |
// Model.allParameters.materializeForSet | |
sil hidden [transparent] @$S5param5ModelV13allParametersAC0D0Vvm : $@convention(method) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout Model) -> (Builtin.RawPointer, Optional<Builtin.RawPointer>) { | |
// %0 // user: %3 | |
// %2 // user: %4 | |
bb0(%0 : $Builtin.RawPointer, %1 : $*Builtin.UnsafeValueBuffer, %2 : $*Model): | |
%3 = pointer_to_address %0 : $Builtin.RawPointer to [strict] $*Model.Parameters // users: %8, %7 | |
%4 = load [trivial] %2 : $*Model // user: %6 | |
// function_ref Model.allParameters.getter | |
%5 = function_ref @$S5param5ModelV13allParametersAC0D0Vvg : $@convention(method) (Model) -> Model.Parameters // user: %6 | |
%6 = apply %5(%4) : $@convention(method) (Model) -> Model.Parameters // user: %7 | |
store %6 to [trivial] %3 : $*Model.Parameters // id: %7 | |
%8 = address_to_pointer %3 : $*Model.Parameters to $Builtin.RawPointer // user: %12 | |
// function_ref closure #1 in Model.allParameters.materializeForSet | |
%9 = function_ref @$S5param5ModelV13allParametersAC0D0VvmytfU_ : $@convention(method) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout Model, @thick Model.Type) -> () // user: %10 | |
%10 = thin_function_to_pointer %9 : $@convention(method) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout Model, @thick Model.Type) -> () to $Builtin.RawPointer // user: %11 | |
%11 = enum $Optional<Builtin.RawPointer>, #Optional.some!enumelt.1, %10 : $Builtin.RawPointer // user: %12 | |
%12 = tuple (%8 : $Builtin.RawPointer, %11 : $Optional<Builtin.RawPointer>) // user: %13 | |
return %12 : $(Builtin.RawPointer, Optional<Builtin.RawPointer>) // id: %13 | |
} // end sil function '$S5param5ModelV13allParametersAC0D0Vvm' | |
// Model.updateParameters(gradients:predicate:) | |
sil hidden @$S5param5ModelV16updateParameters9gradients9predicateyAC0D0V_ySfz_SftXEtF : $@convention(method) (Model.Parameters, @noescape @callee_guaranteed (@inout Float, Float) -> (), @inout Model) -> () { | |
// %0 // users: %6, %3 | |
// %1 // users: %9, %4 | |
// %2 // users: %7, %5 | |
bb0(%0 : $Model.Parameters, %1 : $@noescape @callee_guaranteed (@inout Float, Float) -> (), %2 : $*Model): | |
debug_value %0 : $Model.Parameters, let, name "gradients", argno 1 // id: %3 | |
debug_value %1 : $@noescape @callee_guaranteed (@inout Float, Float) -> (), let, name "predicate", argno 2 // id: %4 | |
debug_value_addr %2 : $*Model, var, name "self", argno 3 // id: %5 | |
%6 = struct_extract %0 : $Model.Parameters, #Model.Parameters.w // user: %9 | |
%7 = begin_access [modify] [unknown] %2 : $*Model // users: %10, %8 | |
%8 = struct_element_addr %7 : $*Model, #Model.w // user: %9 | |
%9 = apply %1(%8, %6) : $@noescape @callee_guaranteed (@inout Float, Float) -> () | |
end_access %7 : $*Model // id: %10 | |
%11 = tuple () // user: %12 | |
return %11 : $() // id: %12 | |
} // end sil function '$S5param5ModelV16updateParameters9gradients9predicateyAC0D0V_ySfz_SftXEtF' | |
// Model.init(w:) | |
sil hidden @$S5param5ModelV1wACSf_tcfC : $@convention(method) (Float, @thin Model.Type) -> Model { | |
// %0 // user: %2 | |
bb0(%0 : $Float, %1 : $@thin Model.Type): | |
%2 = struct $Model (%0 : $Float) // user: %3 | |
return %2 : $Model // id: %3 | |
} // end sil function '$S5param5ModelV1wACSf_tcfC' | |
// optimize(parameters:gradients:) | |
sil hidden @$S5param8optimize10parameters9gradientsyAA5ModelV10ParametersVz_AHtF : $@convention(thin) (@inout Model.Parameters, Model.Parameters) -> () { | |
// %0 // users: %13, %2 | |
// %1 // users: %10, %3 | |
bb0(%0 : $*Model.Parameters, %1 : $Model.Parameters): | |
debug_value_addr %0 : $*Model.Parameters, var, name "parameters", argno 1 // id: %2 | |
debug_value %1 : $Model.Parameters, let, name "gradients", argno 2 // id: %3 | |
%4 = metatype $@thin Float.Type // user: %16 | |
%5 = metatype $@thin Float.Type // user: %12 | |
%6 = metatype $@thin Float.Type // user: %9 | |
%7 = float_literal $Builtin.FPIEEE80, 0x3FFBCCCCCCCCCCCCCCCD // 0.100000000000000000001 // user: %9 | |
// function_ref Float.init(_builtinFloatLiteral:) | |
%8 = function_ref @$SSf20_builtinFloatLiteralSfBf80__tcfC : $@convention(method) (Builtin.FPIEEE80, @thin Float.Type) -> Float // user: %9 | |
%9 = apply %8(%7, %6) : $@convention(method) (Builtin.FPIEEE80, @thin Float.Type) -> Float // user: %12 | |
%10 = struct_extract %1 : $Model.Parameters, #Model.Parameters.w // user: %12 | |
// function_ref static Float.* infix(_:_:) | |
%11 = function_ref @$SSf1moiyS2f_SftFZ : $@convention(method) (Float, Float, @thin Float.Type) -> Float // user: %12 | |
%12 = apply %11(%9, %10, %5) : $@convention(method) (Float, Float, @thin Float.Type) -> Float // user: %16 | |
%13 = begin_access [modify] [unknown] %0 : $*Model.Parameters // users: %17, %14 | |
%14 = struct_element_addr %13 : $*Model.Parameters, #Model.Parameters.w // user: %16 | |
// function_ref static Float.-= infix(_:_:) | |
%15 = function_ref @$SSf2seoiyySfz_SftFZ : $@convention(method) (@inout Float, Float, @thin Float.Type) -> () // user: %16 | |
%16 = apply %15(%14, %12, %4) : $@convention(method) (@inout Float, Float, @thin Float.Type) -> () | |
end_access %13 : $*Model.Parameters // id: %17 | |
%18 = tuple () // user: %19 | |
return %18 : $() // id: %19 | |
} // end sil function '$S5param8optimize10parameters9gradientsyAA5ModelV10ParametersVz_AHtF' | |
// Float.init(_builtinFloatLiteral:) | |
sil [transparent] [serialized] @$SSf20_builtinFloatLiteralSfBf80__tcfC : $@convention(method) (Builtin.FPIEEE80, @thin Float.Type) -> Float | |
// static Float.* infix(_:_:) | |
sil [transparent] [serialized] @$SSf1moiyS2f_SftFZ : $@convention(method) (Float, Float, @thin Float.Type) -> Float | |
// static Float.-= infix(_:_:) | |
sil [transparent] [serialized] @$SSf2seoiyySfz_SftFZ : $@convention(method) (@inout Float, Float, @thin Float.Type) -> () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment