Class: Gsplat::Autograd::Variable
- Inherits:
-
Object
- Object
- Gsplat::Autograd::Variable
- Defined in:
- lib/gsplat/autograd/variable.rb
Overview
A Numo array with an optional reverse-mode gradient and creator node.
Instance Attribute Summary collapse
-
#absgrad ⇒ Object
readonly
Returns the value of attribute absgrad.
-
#creator ⇒ Object
readonly
Returns the value of attribute creator.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#grad ⇒ Object
readonly
Returns the value of attribute grad.
Instance Method Summary collapse
-
#accumulate_absgrad(gradient) ⇒ void
private
Accumulates an auxiliary absolute gradient produced by rasterization.
-
#backward(gradient = nil) ⇒ void
Runs reverse-mode differentiation from this variable.
-
#bind_absgrad(target, key) ⇒ void
private
Binds auxiliary absolute-gradient updates to a metadata hash entry.
-
#clear_creator(node) ⇒ void
private
Removes a creator after its graph node has completed backward.
-
#initialize(data, requires_grad: false, creator: nil) ⇒ Variable
constructor
A new instance of Variable.
-
#replace_data!(value) ⇒ void
private
Replaces parameter storage after a first-axis structural edit.
-
#requires_grad? ⇒ Boolean
Whether this variable accumulates gradients.
-
#zero_grad! ⇒ void
Clears an accumulated leaf gradient.
Constructor Details
#initialize(data, requires_grad: false, creator: nil) ⇒ Variable
Returns a new instance of Variable.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/gsplat/autograd/variable.rb', line 12 def initialize(data, requires_grad: false, creator: nil) raise ArgumentError, "Variable data must be a Numo::NArray" unless data.is_a?(Numo::NArray) @data = data @requires_grad = requires_grad @creator = creator @grad = nil @absgrad = nil @absgrad_targets = [] end |
Instance Attribute Details
#absgrad ⇒ Object (readonly)
Returns the value of attribute absgrad.
7 8 9 |
# File 'lib/gsplat/autograd/variable.rb', line 7 def absgrad @absgrad end |
#creator ⇒ Object (readonly)
Returns the value of attribute creator.
7 8 9 |
# File 'lib/gsplat/autograd/variable.rb', line 7 def creator @creator end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/gsplat/autograd/variable.rb', line 7 def data @data end |
#grad ⇒ Object (readonly)
Returns the value of attribute grad.
7 8 9 |
# File 'lib/gsplat/autograd/variable.rb', line 7 def grad @grad end |
Instance Method Details
#accumulate_absgrad(gradient) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Accumulates an auxiliary absolute gradient produced by rasterization.
44 45 46 47 48 49 50 51 52 |
# File 'lib/gsplat/autograd/variable.rb', line 44 def accumulate_absgrad(gradient) gradient = cast_gradient(gradient) unless gradient.shape == data.shape raise ShapeError, "absgrad shape mismatch: expected #{data.shape.inspect}, got #{gradient.shape.inspect}" end @absgrad = @absgrad ? @absgrad + gradient : gradient.dup @absgrad_targets.each { |target, key| target[key] = @absgrad } end |
#backward(gradient = nil) ⇒ void
This method returns an undefined value.
Runs reverse-mode differentiation from this variable.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/gsplat/autograd/variable.rb', line 88 def backward(gradient = nil) raise Gsplat::Error, "cannot call backward on a variable that does not require gradients" unless requires_grad? accumulate_grad(initial_gradient(gradient)) topological_nodes.reverse_each do |node| grad_outputs = node.outputs.map { |output| output.grad || zeros_like(output.data) } grad_inputs = normalize_grad_inputs(node.backward(*grad_outputs), node.inputs.length) node.inputs.each_with_index do |input, index| next unless input.is_a?(Variable) && node.context.needs_input_grad.fetch(index) input.accumulate_grad(grad_inputs.fetch(index)) end node.release end end |
#bind_absgrad(target, key) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Binds auxiliary absolute-gradient updates to a metadata hash entry.
58 59 60 61 |
# File 'lib/gsplat/autograd/variable.rb', line 58 def bind_absgrad(target, key) @absgrad_targets << [target, key] target[key] = @absgrad end |
#clear_creator(node) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Removes a creator after its graph node has completed backward.
80 81 82 |
# File 'lib/gsplat/autograd/variable.rb', line 80 def clear_creator(node) @creator = nil if @creator.equal?(node) end |
#replace_data!(value) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Replaces parameter storage after a first-axis structural edit.
68 69 70 71 72 73 |
# File 'lib/gsplat/autograd/variable.rb', line 68 def replace_data!(value) raise ArgumentError, "replacement data must be a Numo::NArray" unless value.is_a?(Numo::NArray) @data = value zero_grad! end |
#requires_grad? ⇒ Boolean
Whether this variable accumulates gradients.
26 27 28 |
# File 'lib/gsplat/autograd/variable.rb', line 26 def requires_grad? @requires_grad end |
#zero_grad! ⇒ void
This method returns an undefined value.
Clears an accumulated leaf gradient.
33 34 35 36 37 |
# File 'lib/gsplat/autograd/variable.rb', line 33 def zero_grad! @grad = nil @absgrad = nil @absgrad_targets.each { |target, key| target[key] = nil } end |