Repository: microsoft/knossos-ksc

Issue:
Rename convert_argument
In src/runtime/knossos-entry-points.h, we chose poor names for the functions `convert_argument` and `convert_return_value`, as the names reflect what the functions were originally used for, not what they actually do. 
Rename to `convert_to_ks` and `convert_from_ks`.



Discussion:

Resolving patch:
diff --git a/examples/dl-activations/gelu.py b/examples/dl-activations/gelu.py
--- a/examples/dl-activations/gelu.py
+++ b/examples/dl-activations/gelu.py
@@ -63,17 +63,17 @@
 
 torch::Tensor entry(torch::Tensor t) {
     using namespace ks::entry_points;
-    auto ks_t = convert_argument<ks::tensor<1, ks::Float>>(t);
+    auto ks_t = convert_to_ks_viewing_tensordata<ks::tensor<1, ks::Float>>(t);
     auto ks_ret = ks::vgelu(&g_alloc, ks_t);
-    return convert_return_value<torch::Tensor>(ks_ret);
+    return convert_from_ks<torch::Tensor>(ks_ret);
 }
 
 torch::Tensor entry_vjp(torch::Tensor t, torch::Tensor dret) {
     using namespace ks::entry_points;
-    auto ks_t = convert_argument<ks::tensor<1, ks::Float>>(t);
-    auto ks_dret = convert_argument<ks::tensor<1, ks::Float>>(dret);
+    auto ks_t = convert_to_ks_viewing_tensordata<ks::tensor<1, ks::Float>>(t);
+    auto ks_dret = convert_to_ks_viewing_tensordata<ks::tensor<1, ks::Float>>(dret);
     auto ks_ret = ks::sufrev_vgelu(&g_alloc, ks_t, ks_dret);
-    return convert_return_value<torch::Tensor>(ks_ret);
+    return convert_from_ks<torch::Tensor>(ks_ret);
 }
 """
 
diff --git a/examples/dl-activations/relu3.py b/examples/dl-activations/relu3.py
--- a/examples/dl-activations/relu3.py
+++ b/examples/dl-activations/relu3.py
@@ -281,17 +281,17 @@ def vrelu3_embedded_ks_checkpointed_map_flags():
 
 torch::Tensor entry(torch::Tensor t) {
     using namespace ks::entry_points;
-    auto ks_t = convert_argument<ks::tensor<1, ks::Float>>(t);
+    auto ks_t = convert_to_ks_viewing_tensordata<ks::tensor<1, ks::Float>>(t);
     auto ks_ret = ks::vrelu3(&g_alloc, ks_t);
-    return convert_return_value<torch::Tensor>(ks_ret);
+    return convert_from_ks<torch::Tensor>(ks_ret);
 }
 
 torch::Tensor entry_vjp(torch::Tensor t, torch::Tensor dret) {
     using namespace ks::entry_points;
-    auto ks_t = convert_argument<ks::tensor<1, ks::Float>>(t);
-    auto ks_dret = convert_argument<ks::tensor<1, ks::Float>>(dret);
+    auto ks_t = convert_to_ks_viewing_tensordata<ks::tensor<1, ks::Float>>(t);
+    auto ks_dret = convert_to_ks_viewing_tensordata<ks::tensor<1, ks::Float>>(dret);
     auto ks_ret = ks::sufrev_vrelu3(&g_alloc, ks_t, ks_dret);
-    return convert_return_value<torch::Tensor>(ks_ret);
+    return convert_from_ks<torch::Tensor>(ks_ret);
 }
 """
 
diff --git a/src/python/ksc/cgen.py b/src/python/ksc/cgen.py
--- a/src/python/ksc/cgen.py
+++ b/src/python/ksc/cgen.py
@@ -207,11 +207,11 @@ def join_args(sep, callable):
 {cpp_function} {{
 """
 
-    # auto ks_arg0 = convert_argument<ks::tensor<Dim, Float>>(arg0);
+    # auto ks_arg0 = convert_to_ks_viewing_tensordata<ks::tensor<Dim, Float>>(arg0);
     # ...
-    # auto ks_arg7 = convert_argument<ks::tensor<Dim, Float>>(arg7);
+    # auto ks_arg7 = convert_to_ks_viewing_tensordata<ks::tensor<Dim, Float>>(arg7);
     for i in range(num_args):
-        cpp += f"    auto ks_arg{i} = convert_argument<{ks_cpp_type(arg_types[i])}>(arg{i});\n"
+        cpp += f"    auto ks_arg{i} = convert_to_ks_viewing_tensordata<{ks_cpp_type(arg_types[i])}>(arg{i});\n"
 
     # auto ks_ret = ks::my_kernel(&g_alloc, ks_arg0, ..., ks_arg7);
     cpp += f"""
@@ -220,7 +220,7 @@ def join_args(sep, callable):
 
     # convert return value and return
     cpp += f"""
-    return convert_return_value<{cpp_return_type}>(ks_ret);
+    return convert_from_ks<{cpp_return_type}>(ks_ret);
 }}
 """
     return cpp_declaration, cpp
@@ -343,16 +343,16 @@ def concat_args(callable):
     int64_t n = arg0.size(0);
 """
 
-    # auto ks_arg0 = convert_argument<ks::tensor<2,float>>(arg0)
+    # auto ks_arg0 = convert_to_ks_viewing_tensordata<ks::tensor<2,float>>(arg0)
     # ...
-    # auto ks_arg7 = convert_argument<ks::tensor<1,int>>(arg7)
+    # auto ks_arg7 = convert_to_ks_viewing_tensordata<ks::tensor<1,int>>(arg7)
     for k in range(num_args):
         cpp += f"""
     KS_ASSERT(arg{k}.is_contiguous());
     KS_ASSERT(arg{k}.scalar_type() == scalar_type_of_Float);
     KS_ASSERT(arg{k}.size(0) == n);
 
-    auto ks_arg{k} = convert_argument<{ks_types[k]}>(arg{k});
+    auto ks_arg{k} = convert_to_ks_viewing_tensordata<{ks_types[k]}>(arg{k});
 """
 
     # Difficulty: depending on the rank of ks_ret, ks_ret[i] returns either
@@ -390,8 +390,8 @@ def concat_args(callable):
     auto [{ks_sizes}] = ret0.size();
     // TODO: use torch::empty here, and copydown below.
     auto ret = torch::zeros({{n, {ks_sizes}}});
-    // And wrap it in ks - this is a view of the torch data, so convert_argument, not convert_return_value
-    auto ks_ret = convert_argument<ks::tensor<{ks_return_dim}, Float>>(ret);
+    // And view it as a ks tensor
+    auto ks_ret = convert_to_ks_viewing_tensordata<ks::tensor<{ks_return_dim}, Float>>(ret);
 
     // Place 0th value in the output
     auto ks_ret0 = ks_ret[0];


Test patch: