Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/csrc/training.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ void train_multiarg(const char* name, torchfort_tensor_list_t inputs_in, torchfo

inputs->reset();
labels->reset();
if (extra_loss_args)
extra_loss_args->reset();

torchfort::nvtx::rangePop();
}
Expand Down
6 changes: 3 additions & 3 deletions tests/supervised/scripts/setup_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ def __init__(self):
super(Loss1, self).__init__()

def forward(self, prediction, label):
return (torch.sum(prediction) + torch.sum(label)) / (2 * prediction.numel())
return (torch.sum(prediction) + torch.sum(label))

class Loss2(torch.nn.Module):
def __init__(self):
super(Loss2, self).__init__()

def forward(self, prediction1, prediction2, label1, label2):
return (torch.sum(prediction1) + torch.sum(prediction2) + torch.sum(label1) + torch.sum(label2)) / (4 * prediction1.numel())
return (torch.sum(prediction1) + torch.sum(prediction2) + torch.sum(label1) + torch.sum(label2))

class Loss2Extra(torch.nn.Module):
def __init__(self):
super(Loss2Extra, self).__init__()

def forward(self, prediction1, prediction2, label1, label2, extra_args1, extra_args2):
return (torch.sum(prediction1) + torch.sum(prediction2) + torch.sum(label1) + torch.sum(label2) +
torch.sum(extra_args1) + torch.sum(extra_args2)) / (6 * prediction1.numel())
torch.sum(extra_args1) + torch.sum(extra_args2))

def main():
model1 = Net1()
Expand Down
25 changes: 15 additions & 10 deletions tests/supervised/test_training.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,26 +220,31 @@ void training_test_multiarg(const std::string& model_config, int dev_model, int
}

// Check that external data changes reflect in tensor list
float expected_loss_val = 0.0;
for (int i = 0; i < 2; ++i) {
auto tmp = generate_random<float>(shape);
auto tmp = generate_constant<float>(shape, 1);
inputs[i].assign(tmp.begin(), tmp.end());
labels[i].assign(tmp.begin(), tmp.end());
expected_loss_val += 2 * 1.0f * tmp.size();
if (use_extra_args) {
extra_args[i].assign(tmp.begin(), tmp.end());
expected_loss_val += 1.0f * tmp.size();
}
#ifdef ENABLE_GPU
if (dev_input != TORCHFORT_DEVICE_CPU) {
copy_from_host_vector(input_ptrs[i], inputs[i]);
copy_from_host_vector(label_ptrs[i], labels[i]);
if (use_extra_args) {
copy_from_host_vector(extra_args_ptrs[i], extra_args[i]);
}
}
#endif
}

CHECK_TORCHFORT(torchfort_inference_multiarg(model_name.c_str(), inputs_tl, outputs_tl, 0));
CHECK_TORCHFORT(torchfort_train_multiarg(model_name.c_str(), inputs_tl, labels_tl, &loss_val,
(use_extra_args) ? extra_args_tl : nullptr, 0));

for (int i = 0; i < 2; ++i) {
#ifdef ENABLE_GPU
if (dev_input != TORCHFORT_DEVICE_CPU) {
copy_to_host_vector(outputs[i], output_ptrs[i]);
}
#endif
EXPECT_EQ(inputs[i], outputs[i]);
}
EXPECT_EQ(loss_val, expected_loss_val);
}

for (int i = 0; i < 2; ++i) {
Expand Down
9 changes: 9 additions & 0 deletions tests/test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ template <typename T> std::vector<T> generate_random(const std::vector<int64_t>&
return data;
}

// Generate constant vector data for testing
template <typename T> std::vector<T> generate_constant(const std::vector<int64_t>& shape, T value) {

int64_t num_values = std::accumulate(shape.begin(), shape.end(), 1, std::multiplies<int64_t>());
std::vector<T> data(num_values, value);

return data;
}

// Generate random names to use as model keys to avoid conflicts between tests
std::string generate_random_name(int length) {

Expand Down