Skip to content

Commit 2609a3f

Browse files
committed
Factor out write_torch_registration_macros to common mod
1 parent 01bb2c7 commit 2609a3f

5 files changed

Lines changed: 19 additions & 48 deletions

File tree

build2cmake/src/torch/common.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::PathBuf;
2+
13
use eyre::{Context, Result};
24
use itertools::Itertools;
35
use minijinja::{context, Environment};
@@ -6,6 +8,8 @@ use crate::config::{Backend, General};
68
use crate::metadata::Metadata;
79
use crate::FileSet;
810

11+
static REGISTRATION_H: &str = include_str!("../templates/registration.h");
12+
913
pub fn write_pyproject_toml(
1014
env: &Environment,
1115
backend: Backend,
@@ -64,3 +68,14 @@ where
6468
.collect_vec()
6569
.join(";")
6670
}
71+
72+
pub fn write_torch_registration_macros(file_set: &mut FileSet) -> Result<()> {
73+
let mut path = PathBuf::new();
74+
path.push("torch-ext");
75+
path.push("registration.h");
76+
file_set
77+
.entry(path)
78+
.extend_from_slice(REGISTRATION_H.as_bytes());
79+
80+
Ok(())
81+
}

build2cmake/src/torch/cpu.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ use crate::config::{Backend, Build, Torch};
88
use crate::fileset::FileSet;
99
use crate::torch::common::write_metadata;
1010
use crate::torch::common::write_pyproject_toml;
11+
use crate::torch::common::write_torch_registration_macros;
1112
use crate::torch::kernel::render_kernel_components;
1213
use crate::torch::kernel_ops_identifier;
1314
use crate::version::Version;
1415

1516
static CMAKE_UTILS: &str = include_str!("../templates/utils.cmake");
1617
static CMAKE_KERNEL: &str = include_str!("../templates/kernel.cmake");
17-
static REGISTRATION_H: &str = include_str!("../templates/registration.h");
1818

1919
pub fn write_torch_ext_cpu(
2020
env: &Environment,
@@ -224,17 +224,6 @@ fn write_setup_py(
224224
Ok(())
225225
}
226226

227-
fn write_torch_registration_macros(file_set: &mut FileSet) -> Result<()> {
228-
let mut path = PathBuf::new();
229-
path.push("torch-ext");
230-
path.push("registration.h");
231-
file_set
232-
.entry(path)
233-
.extend_from_slice(REGISTRATION_H.as_bytes());
234-
235-
Ok(())
236-
}
237-
238227
fn prefix_and_join_includes<S>(includes: impl AsRef<[S]>) -> String
239228
where
240229
S: AsRef<str>,

build2cmake/src/torch/cuda.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::config::{Backend, Build, Dependency, Torch};
1010
use crate::torch::common::prefix_and_join_includes;
1111
use crate::torch::common::write_metadata;
1212
use crate::torch::common::write_pyproject_toml;
13+
use crate::torch::common::write_torch_registration_macros;
1314
use crate::torch::kernel::render_kernel_components;
1415
use crate::torch::kernel_ops_identifier;
1516
use crate::version::Version;
@@ -18,7 +19,6 @@ use crate::FileSet;
1819
static CMAKE_UTILS: &str = include_str!("../templates/utils.cmake");
1920
static CMAKE_KERNEL: &str = include_str!("../templates/kernel.cmake");
2021
static WINDOWS_UTILS: &str = include_str!("../templates/windows.cmake");
21-
static REGISTRATION_H: &str = include_str!("../templates/registration.h");
2222
static HIPIFY: &str = include_str!("../templates/cuda/hipify.py");
2323

2424
pub fn write_torch_ext_cuda(
@@ -66,17 +66,6 @@ pub fn write_torch_ext_cuda(
6666
Ok(file_set)
6767
}
6868

69-
fn write_torch_registration_macros(file_set: &mut FileSet) -> Result<()> {
70-
let mut path = PathBuf::new();
71-
path.push("torch-ext");
72-
path.push("registration.h");
73-
file_set
74-
.entry(path)
75-
.extend_from_slice(REGISTRATION_H.as_bytes());
76-
77-
Ok(())
78-
}
79-
8069
fn write_setup_py(
8170
env: &Environment,
8271
torch: &Torch,

build2cmake/src/torch/metal.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ use crate::config::{Backend, Build, Torch};
88
use crate::fileset::FileSet;
99
use crate::torch::common::write_metadata;
1010
use crate::torch::common::write_pyproject_toml;
11+
use crate::torch::common::write_torch_registration_macros;
1112
use crate::torch::kernel::render_kernel_components;
1213
use crate::torch::kernel_ops_identifier;
1314
use crate::version::Version;
1415

1516
static CMAKE_UTILS: &str = include_str!("../templates/utils.cmake");
1617
static CMAKE_KERNEL: &str = include_str!("../templates/kernel.cmake");
17-
static REGISTRATION_H: &str = include_str!("../templates/registration.h");
1818
static COMPILE_METAL_CMAKE: &str = include_str!("../templates/metal/compile-metal.cmake");
1919
static METALLIB_TO_HEADER_PY: &str = include_str!("../templates/metal/metallib_to_header.py");
2020

@@ -240,17 +240,6 @@ fn write_setup_py(
240240
Ok(())
241241
}
242242

243-
fn write_torch_registration_macros(file_set: &mut FileSet) -> Result<()> {
244-
let mut path = PathBuf::new();
245-
path.push("torch-ext");
246-
path.push("registration.h");
247-
file_set
248-
.entry(path)
249-
.extend_from_slice(REGISTRATION_H.as_bytes());
250-
251-
Ok(())
252-
}
253-
254243
fn prefix_and_join_includes<S>(includes: impl AsRef<[S]>) -> String
255244
where
256245
S: AsRef<str>,

build2cmake/src/torch/xpu.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ use minijinja::{context, Environment};
99
use crate::config::{Backend, Build, Dependency, Torch};
1010
use crate::torch::common::write_metadata;
1111
use crate::torch::common::write_pyproject_toml;
12+
use crate::torch::common::write_torch_registration_macros;
1213
use crate::torch::kernel::render_kernel_components;
1314
use crate::torch::kernel_ops_identifier;
1415
use crate::version::Version;
1516
use crate::FileSet;
1617

1718
static CMAKE_UTILS: &str = include_str!("../templates/utils.cmake");
1819
static CMAKE_KERNEL: &str = include_str!("../templates/kernel.cmake");
19-
static REGISTRATION_H: &str = include_str!("../templates/registration.h");
2020
static WINDOWS_UTILS: &str = include_str!("../templates/windows.cmake");
2121

2222
pub fn write_torch_ext_xpu(
@@ -62,17 +62,6 @@ pub fn write_torch_ext_xpu(
6262
Ok(file_set)
6363
}
6464

65-
fn write_torch_registration_macros(file_set: &mut FileSet) -> Result<()> {
66-
let mut path = PathBuf::new();
67-
path.push("torch-ext");
68-
path.push("registration.h");
69-
file_set
70-
.entry(path)
71-
.extend_from_slice(REGISTRATION_H.as_bytes());
72-
73-
Ok(())
74-
}
75-
7665
fn write_setup_py(
7766
env: &Environment,
7867
torch: &Torch,

0 commit comments

Comments
 (0)