Class: Gsplat::Autograd::Variable

Inherits:
Object
  • Object
show all
Defined in:
lib/gsplat/autograd/variable.rb

Overview

A Numo array with an optional reverse-mode gradient and creator node.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, requires_grad: false, creator: nil) ⇒ Variable

Returns a new instance of Variable.

Parameters:

  • data (Numo::NArray)

    tensor data

  • requires_grad (Boolean) (defaults to: false)

    whether to accumulate a gradient

  • creator (GraphNode, nil) (defaults to: nil)

    producing operation

Raises:

  • (ArgumentError)


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

#absgradObject (readonly)

Returns the value of attribute absgrad.



7
8
9
# File 'lib/gsplat/autograd/variable.rb', line 7

def absgrad
  @absgrad
end

#creatorObject (readonly)

Returns the value of attribute creator.



7
8
9
# File 'lib/gsplat/autograd/variable.rb', line 7

def creator
  @creator
end

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/gsplat/autograd/variable.rb', line 7

def data
  @data
end

#gradObject (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.

Parameters:

  • gradient (Numo::NArray)


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.

Parameters:

  • gradient (Numo::NArray, Numeric, nil) (defaults to: nil)

    output gradient; nil is valid for scalar output

Raises:



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.

Parameters:



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.

Parameters:

  • value (Numo::NArray)

Raises:

  • (ArgumentError)


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.

Returns:

  • (Boolean)


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