Skip to content

Commit

Permalink
[bugfix] Resolve segfault in tensor apply
Browse files Browse the repository at this point in the history
This PR fixes a bug where a segmentation fault occurs when the output tensor is empty.

The fix initializes the output tensor when empty to avoid this error.

**Self-evaluation:**
1. Build test: [X]Passed [ ]Failed [ ]Skipped
2. Run test:   [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: Donghyeon Jeong <[email protected]>
  • Loading branch information
djeong20 committed Jan 23, 2024
1 parent 07db2d5 commit 270b106
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
2 changes: 2 additions & 0 deletions nntrainer/tensor/float_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ void FloatTensor::initialize(Initializer init) {

TensorV2 &FloatTensor::apply(std::function<float(float)> f,
TensorV2 &output) const {
CREATE_V2_IF_EMPTY_DIMS(output, dim, nullptr);

if (contiguous && output.getContiguous()) {
const float *data = (float *)getData();
float *rdata = output.getData<float>();
Expand Down
2 changes: 2 additions & 0 deletions nntrainer/tensor/half_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ void HalfTensor::initialize(Initializer init) {

TensorV2 &HalfTensor::apply(std::function<_FP16(_FP16)> f,
TensorV2 &output) const {
CREATE_V2_IF_EMPTY_DIMS(output, dim, nullptr);

if (contiguous && output.getContiguous()) {
const _FP16 *data = (_FP16 *)getData();
_FP16 *rdata = output.getData<_FP16>();
Expand Down

0 comments on commit 270b106

Please sign in to comment.