Skip to content
This repository was archived by the owner on Jun 11, 2026. It is now read-only.

Use torch::tensor instead of ks::tensor in entry points#931

Merged
dcrc2 merged 5 commits into
masterfrom
dcrc2/torch-tensor-entry-points
Jul 16, 2021
Merged

Use torch::tensor instead of ks::tensor in entry points#931
dcrc2 merged 5 commits into
masterfrom
dcrc2/torch-tensor-entry-points

Conversation

@dcrc2

@dcrc2 dcrc2 commented Jul 9, 2021

Copy link
Copy Markdown
Contributor

AB#19887

Draft PR: the current version breaks some of the python tests. fixed

Move the conversions between torch types and ks types into C++ code (generated by python/ksc/cgen.py). Example code generated for relu3:

namespace ks {
namespace entry_points {
namespace generated {

torch::Tensor entry(torch::Tensor arg0) {
    if (g_logging) {
        std::cerr << "vrelu3$aT1f(" << arg0 << ") =" << std::endl;
    }
    auto ks_arg0 = convert_argument<ks::tensor<1, ks::Float>>(arg0);
    auto ks_ret = ks::vrelu3$aT1f(&g_alloc, ks_arg0);
    auto ret = convert_return_value<torch::Tensor>(ks_ret);
    if (g_logging) {
        std::cerr << ret << std::endl;
    }
    return ret;
}

torch::Tensor entry_vjp(torch::Tensor arg0, torch::Tensor arg1) {
    if (g_logging) {
        std::cerr << "sufrev$vrelu3$aT1f(" << arg0 << ", "  << arg1 << ") =" << std::endl;
    }
    auto ks_arg0 = convert_argument<ks::tensor<1, ks::Float>>(arg0);
    auto ks_arg1 = convert_argument<ks::tensor<1, ks::Float>>(arg1);
    auto ks_ret = ks::sufrev$vrelu3$aT1f(&g_alloc, ks_arg0, ks_arg1);
    auto ret = convert_return_value<torch::Tensor>(ks_ret);
    if (g_logging) {
        std::cerr << ret << std::endl;
    }
    return ret;
}

}
}
}

@awf awf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fab, thanks.

We might one day want to passs numpy arrays to KS, I don't know if that has any implications for the code we keep.

Comment thread src/python/ksc/cgen.py Outdated
Comment thread src/python/ksc/compile.py Outdated
Comment on lines -190 to -192
declare_tensor_1<ks::Float>(m, "Tensor_1_Float");
declare_tensor_2<ks::Float>(m, "Tensor_2_Float");
declare_tensor_2<int>(m, "Tensor_2_Integer");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay!!

@dcrc2
dcrc2 force-pushed the dcrc2/torch-tensor-entry-points branch from 45c7144 to c2d5268 Compare July 12, 2021 11:02
@dcrc2
dcrc2 force-pushed the dcrc2/generate-cpp-entry-points branch from a90a4af to c73aa2c Compare July 12, 2021 15:03
@dcrc2
dcrc2 force-pushed the dcrc2/torch-tensor-entry-points branch from 8f45fac to 9d9571b Compare July 12, 2021 16:12
@dcrc2
dcrc2 marked this pull request as ready for review July 12, 2021 16:12
@dcrc2 dcrc2 mentioned this pull request Jul 13, 2021
@dcrc2
dcrc2 force-pushed the dcrc2/generate-cpp-entry-points branch 2 times, most recently from e6a2ef1 to b92b82f Compare July 13, 2021 12:36
@dcrc2
dcrc2 force-pushed the dcrc2/torch-tensor-entry-points branch 2 times, most recently from 6c28b5f to 387dda2 Compare July 13, 2021 13:09
@dcrc2
dcrc2 force-pushed the dcrc2/generate-cpp-entry-points branch from b92b82f to 4da90f2 Compare July 13, 2021 15:01
@dcrc2
dcrc2 force-pushed the dcrc2/torch-tensor-entry-points branch 2 times, most recently from 05d499c to d114505 Compare July 13, 2021 17:20
@dcrc2 dcrc2 mentioned this pull request Jul 15, 2021
@dcrc2
dcrc2 marked this pull request as draft July 15, 2021 09:16
Base automatically changed from dcrc2/generate-cpp-entry-points to master July 15, 2021 09:54
@dcrc2
dcrc2 force-pushed the dcrc2/torch-tensor-entry-points branch from d114505 to 2b74bb5 Compare July 15, 2021 11:27
@dcrc2
dcrc2 force-pushed the dcrc2/torch-tensor-entry-points branch from 2b74bb5 to 4184b70 Compare July 15, 2021 11:28
@dcrc2

dcrc2 commented Jul 15, 2021

Copy link
Copy Markdown
Contributor Author

An additional complication: we have some tests (test/python/test_tracing_core.py) which use ksc-generated code without PyTorch. In order to support this, I've moved the torch-specific code to a separate header (knossos-entry-points-torch.h), so that we include either knossos-entry-points.h or knossos-entry-points-torch.h as appropriate. In non-PyTorch mode it is an error to define an entry point which uses a tensor.

At the moment this is all done by adding a new boolean parameter use_torch to the C++-generating functions. In future there might be more than two choices here (supporting bindings to various languages/libraries), so we might want to upgrade this parameter to an abstract base class.

@dcrc2
dcrc2 requested a review from toelli-msft July 15, 2021 11:51
@dcrc2
dcrc2 marked this pull request as ready for review July 15, 2021 11:51

@awf awf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy with the bool, we can generalize later if neccesary

@dcrc2
dcrc2 merged commit 1305e89 into master Jul 16, 2021
@dcrc2
dcrc2 deleted the dcrc2/torch-tensor-entry-points branch July 16, 2021 13:13
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants