-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
set nil as the initial value of the struct reference type fields #51
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,8 +75,8 @@ pub fn (g &SubstractGate[T]) cache[T](mut result Variable[T], args ...CacheParam | |
|
||
pub struct MultiplyGate[T] { | ||
pub: | ||
a &Variable[T] | ||
b &Variable[T] | ||
a &Variable[T] = unsafe { nil } | ||
b &Variable[T] = unsafe { nil } | ||
} | ||
|
||
pub fn multiply_gate[T](a &Variable[T], b &Variable[T]) &MultiplyGate[T] { | ||
|
@@ -119,8 +119,8 @@ pub fn (g &MultiplyGate[T]) cache[T](mut result Variable[T], args ...CacheParam) | |
|
||
pub struct DivideGate[T] { | ||
pub: | ||
a &Variable[T] | ||
b &Variable[T] | ||
a &Variable[T] = unsafe { nil } | ||
b &Variable[T] = unsafe { nil } | ||
} | ||
Comment on lines
120
to
124
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the |
||
|
||
pub fn divide_gate[T](a &Variable[T], b &Variable[T]) &DivideGate[T] { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,8 @@ import vtl.la | |
|
||
pub struct MatMulGate[T] { | ||
pub: | ||
a &Variable[T] | ||
b &Variable[T] | ||
a &Variable[T] = unsafe { nil } | ||
b &Variable[T] = unsafe { nil } | ||
} | ||
Comment on lines
6
to
10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The initialization of |
||
|
||
pub fn matmul_gate[T](a &Variable[T], b &Variable[T]) &MatMulGate[T] { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,8 @@ import math | |
import vtl | ||
|
||
pub struct PowGate[T] { | ||
a &Variable[T] | ||
b &Variable[T] | ||
a &Variable[T] = unsafe { nil } | ||
b &Variable[T] = unsafe { nil } | ||
} | ||
|
||
pub fn pow_gate[T](a &Variable[T], b &Variable[T]) &PowGate[T] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ module autograd | |
pub struct Payload[T] { | ||
pub: | ||
// Contents of the paylod | ||
variable &Variable[T] | ||
variable &Variable[T] = unsafe { nil } | ||
} | ||
Comment on lines
7
to
11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change to initialize the |
||
|
||
pub fn payload[T](variable &Variable[T]) &Payload[T] { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,15 +15,15 @@ pub mut: | |
// The value of the Variable. This should not be edited outside | ||
// of Variable operations, as other edits will not be tracked | ||
// and will lead to incorrect results | ||
value &vtl.Tensor[T] | ||
value &vtl.Tensor[T] = unsafe { nil } | ||
// The graph the variable is associated with. This is a reference, | ||
// as a variable does not own its context | ||
context &Context[T] | ||
context &Context[T] = unsafe { nil } | ||
// The gradient of the Variable. This is set as a reference to | ||
// the value of a Variable unless `backprop` has been called, in | ||
// which case all related Variables will have their gradient | ||
// updated correctly | ||
grad &vtl.Tensor[T] | ||
grad &vtl.Tensor[T] = unsafe { nil } | ||
// If set to true, this variable will track its operations, | ||
// otherwise it will act similar to a vtl.Tensor, only calculating | ||
// forward operations | ||
Comment on lines
15
to
29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,9 @@ import vtl.stats | |
|
||
pub struct LinearGate[T] { | ||
pub: | ||
input &autograd.Variable[T] | ||
weight &autograd.Variable[T] | ||
bias &autograd.Variable[T] | ||
input &autograd.Variable[T] = unsafe { nil } | ||
weight &autograd.Variable[T] = unsafe { nil } | ||
bias &autograd.Variable[T] = unsafe { nil } | ||
} | ||
Comment on lines
8
to
13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The initialization of |
||
|
||
pub fn linear_gate[T](input &autograd.Variable[T], weight &autograd.Variable[T], bias &autograd.Variable[T]) &LinearGate[T] { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import vtl.nn.internal | |
|
||
pub struct MaxPool2DGate[T] { | ||
pub: | ||
max_indices &vtl.Tensor[int] | ||
max_indices &vtl.Tensor[int] = unsafe { nil } | ||
kernel []int | ||
shape []int | ||
stride []int | ||
Comment on lines
7
to
12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The initialization of |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,8 @@ import vtl.nn.internal | |
|
||
pub struct SoftmaxCrossEntropyGate[T] { | ||
pub: | ||
cache &autograd.Variable[T] | ||
target &vtl.Tensor[T] | ||
cache &autograd.Variable[T] = unsafe { nil } | ||
target &vtl.Tensor[T] = unsafe { nil } | ||
Comment on lines
+9
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The initialization of |
||
} | ||
|
||
pub fn softmax_cross_entropy_gate[T](cache &autograd.Variable[T], target &vtl.Tensor[T]) &SoftmaxCrossEntropyGate[T] { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import vtl.nn.loss | |
import vtl.nn.types | ||
|
||
pub struct SequentialInfo[T] { | ||
ctx &autograd.Context[T] | ||
ctx &autograd.Context[T] = unsafe { nil } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change to initialize |
||
pub mut: | ||
layers []types.Layer[T] | ||
loss types.Loss | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ pub enum IteratorStrategy { | |
@[heap] | ||
pub struct TensorIterator[T] { | ||
pub: | ||
tensor &Tensor[T] | ||
tensor &Tensor[T] = unsafe { nil } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The initialization of |
||
next_handler IteratorStrategy | ||
pub mut: | ||
iteration int | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ module vtl | |
@[heap] | ||
pub struct TensorAxisIterator[T] { | ||
pub: | ||
tensor &Tensor[T] | ||
tensor &Tensor[T] = unsafe { nil } | ||
pub mut: | ||
shape []int | ||
strides []int | ||
Comment on lines
5
to
11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change to initialize the |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import vtl.storage | |
@[heap] | ||
pub struct VclTensor[T] { | ||
pub mut: | ||
data &storage.VclStorage[T] | ||
data &storage.VclStorage[T] = unsafe { nil } | ||
memory MemoryFormat | ||
size int | ||
shape []int | ||
Comment on lines
6
to
12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The initialization of |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The initialization of
a
andb
tonil
using unsafe blocks is a significant change that addresses potential issues with uninitialized memory. However, it is important to ensure that all code that creates instances ofMultiplyGate
properly initializes these fields before they are used. Failing to do so could lead to null pointer dereferences, which can cause crashes or other undefined behavior. It is also worth considering if there are safer ways to ensure initialization without resorting to unsafe blocks, such as providing default values or factory functions that guarantee proper initialization.