Skip to content

Commit db93aac

Browse files
committed
Add support for PatchTSMixer and other PatchTST classes
1 parent 4b13714 commit db93aac

File tree

4 files changed

+140
-11
lines changed

4 files changed

+140
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ You can refine your search by selecting the task you're interested in (e.g., [te
370370
1. **[OPT](https://huggingface.co/docs/transformers/master/model_doc/opt)** (from Meta AI) released with the paper [OPT: Open Pre-trained Transformer Language Models](https://arxiv.org/abs/2205.01068) by Susan Zhang, Stephen Roller, Naman Goyal, Mikel Artetxe, Moya Chen, Shuohui Chen et al.
371371
1. **[OWL-ViT](https://huggingface.co/docs/transformers/model_doc/owlvit)** (from Google AI) released with the paper [Simple Open-Vocabulary Object Detection with Vision Transformers](https://arxiv.org/abs/2205.06230) by Matthias Minderer, Alexey Gritsenko, Austin Stone, Maxim Neumann, Dirk Weissenborn, Alexey Dosovitskiy, Aravindh Mahendran, Anurag Arnab, Mostafa Dehghani, Zhuoran Shen, Xiao Wang, Xiaohua Zhai, Thomas Kipf, and Neil Houlsby.
372372
1. **[OWLv2](https://huggingface.co/docs/transformers/model_doc/owlv2)** (from Google AI) released with the paper [Scaling Open-Vocabulary Object Detection](https://arxiv.org/abs/2306.09683) by Matthias Minderer, Alexey Gritsenko, Neil Houlsby.
373+
1. **[PatchTSMixer](https://huggingface.co/docs/transformers/main/model_doc/patchtsmixer)** (from IBM) released with the paper [TSMixer: Lightweight MLP-Mixer Model for Multivariate Time Series Forecasting](https://arxiv.org/abs/2306.09364) by Vijay Ekambaram, Arindam Jati, Nam Nguyen, Phanwadee Sinthong, Jayant Kalagnanam.
373374
1. **[PatchTST](https://huggingface.co/docs/transformers/main/model_doc/patchtst)** (from Princeton University, IBM) released with the paper [A Time Series is Worth 64 Words: Long-term Forecasting with Transformers](https://arxiv.org/abs/2211.14730) by Yuqi Nie, Nam H. Nguyen, Phanwadee Sinthong, Jayant Kalagnanam.
374375
1. **[Phi](https://huggingface.co/docs/transformers/main/model_doc/phi)** (from Microsoft) released with the papers - [Textbooks Are All You Need](https://arxiv.org/abs/2306.11644) by Suriya Gunasekar, Yi Zhang, Jyoti Aneja, Caio César Teodoro Mendes, Allie Del Giorno, Sivakanth Gopi, Mojan Javaheripi, Piero Kauffmann, Gustavo de Rosa, Olli Saarikivi, Adil Salim, Shital Shah, Harkirat Singh Behl, Xin Wang, Sébastien Bubeck, Ronen Eldan, Adam Tauman Kalai, Yin Tat Lee and Yuanzhi Li, [Textbooks Are All You Need II: phi-1.5 technical report](https://arxiv.org/abs/2309.05463) by Yuanzhi Li, Sébastien Bubeck, Ronen Eldan, Allie Del Giorno, Suriya Gunasekar and Yin Tat Lee.
375376
1. **[Phi3](https://huggingface.co/docs/transformers/main/model_doc/phi3)** (from Microsoft) released with the paper [Phi-3 Technical Report: A Highly Capable Language Model Locally on Your Phone](https://arxiv.org/abs/2404.14219) by Marah Abdin, Sam Ade Jacobs, Ammar Ahmad Awan, Jyoti Aneja, Ahmed Awadallah, Hany Awadalla, Nguyen Bach, Amit Bahree, Arash Bakhtiari, Harkirat Behl, Alon Benhaim, Misha Bilenko, Johan Bjorck, Sébastien Bubeck, Martin Cai, Caio César Teodoro Mendes, Weizhu Chen, Vishrav Chaudhary, Parul Chopra, Allie Del Giorno, Gustavo de Rosa, Matthew Dixon, Ronen Eldan, Dan Iter, Amit Garg, Abhishek Goswami, Suriya Gunasekar, Emman Haider, Junheng Hao, Russell J. Hewett, Jamie Huynh, Mojan Javaheripi, Xin Jin, Piero Kauffmann, Nikos Karampatziakis, Dongwoo Kim, Mahoud Khademi, Lev Kurilenko, James R. Lee, Yin Tat Lee, Yuanzhi Li, Chen Liang, Weishung Liu, Eric Lin, Zeqi Lin, Piyush Madan, Arindam Mitra, Hardik Modi, Anh Nguyen, Brandon Norick, Barun Patra, Daniel Perez-Becker, Thomas Portet, Reid Pryzant, Heyang Qin, Marko Radmilac, Corby Rosset, Sambudha Roy, Olatunji Ruwase, Olli Saarikivi, Amin Saied, Adil Salim, Michael Santacroce, Shital Shah, Ning Shang, Hiteshi Sharma, Xia Song, Masahiro Tanaka, Xin Wang, Rachel Ward, Guanhua Wang, Philipp Witte, Michael Wyatt, Can Xu, Jiahang Xu, Sonali Yadav, Fan Yang, Ziyi Yang, Donghan Yu, Chengruidong Zhang, Cyril Zhang, Jianwen Zhang, Li Lyna Zhang, Yi Zhang, Yue Zhang, Yunan Zhang, Xiren Zhou.

docs/snippets/6_supported-models.snippet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
1. **[OPT](https://huggingface.co/docs/transformers/master/model_doc/opt)** (from Meta AI) released with the paper [OPT: Open Pre-trained Transformer Language Models](https://arxiv.org/abs/2205.01068) by Susan Zhang, Stephen Roller, Naman Goyal, Mikel Artetxe, Moya Chen, Shuohui Chen et al.
8686
1. **[OWL-ViT](https://huggingface.co/docs/transformers/model_doc/owlvit)** (from Google AI) released with the paper [Simple Open-Vocabulary Object Detection with Vision Transformers](https://arxiv.org/abs/2205.06230) by Matthias Minderer, Alexey Gritsenko, Austin Stone, Maxim Neumann, Dirk Weissenborn, Alexey Dosovitskiy, Aravindh Mahendran, Anurag Arnab, Mostafa Dehghani, Zhuoran Shen, Xiao Wang, Xiaohua Zhai, Thomas Kipf, and Neil Houlsby.
8787
1. **[OWLv2](https://huggingface.co/docs/transformers/model_doc/owlv2)** (from Google AI) released with the paper [Scaling Open-Vocabulary Object Detection](https://arxiv.org/abs/2306.09683) by Matthias Minderer, Alexey Gritsenko, Neil Houlsby.
88+
1. **[PatchTSMixer](https://huggingface.co/docs/transformers/main/model_doc/patchtsmixer)** (from IBM) released with the paper [TSMixer: Lightweight MLP-Mixer Model for Multivariate Time Series Forecasting](https://arxiv.org/abs/2306.09364) by Vijay Ekambaram, Arindam Jati, Nam Nguyen, Phanwadee Sinthong, Jayant Kalagnanam.
8889
1. **[PatchTST](https://huggingface.co/docs/transformers/main/model_doc/patchtst)** (from Princeton University, IBM) released with the paper [A Time Series is Worth 64 Words: Long-term Forecasting with Transformers](https://arxiv.org/abs/2211.14730) by Yuqi Nie, Nam H. Nguyen, Phanwadee Sinthong, Jayant Kalagnanam.
8990
1. **[Phi](https://huggingface.co/docs/transformers/main/model_doc/phi)** (from Microsoft) released with the papers - [Textbooks Are All You Need](https://arxiv.org/abs/2306.11644) by Suriya Gunasekar, Yi Zhang, Jyoti Aneja, Caio César Teodoro Mendes, Allie Del Giorno, Sivakanth Gopi, Mojan Javaheripi, Piero Kauffmann, Gustavo de Rosa, Olli Saarikivi, Adil Salim, Shital Shah, Harkirat Singh Behl, Xin Wang, Sébastien Bubeck, Ronen Eldan, Adam Tauman Kalai, Yin Tat Lee and Yuanzhi Li, [Textbooks Are All You Need II: phi-1.5 technical report](https://arxiv.org/abs/2309.05463) by Yuanzhi Li, Sébastien Bubeck, Ronen Eldan, Allie Del Giorno, Suriya Gunasekar and Yin Tat Lee.
9091
1. **[Phi3](https://huggingface.co/docs/transformers/main/model_doc/phi3)** (from Microsoft) released with the paper [Phi-3 Technical Report: A Highly Capable Language Model Locally on Your Phone](https://arxiv.org/abs/2404.14219) by Marah Abdin, Sam Ade Jacobs, Ammar Ahmad Awan, Jyoti Aneja, Ahmed Awadallah, Hany Awadalla, Nguyen Bach, Amit Bahree, Arash Bakhtiari, Harkirat Behl, Alon Benhaim, Misha Bilenko, Johan Bjorck, Sébastien Bubeck, Martin Cai, Caio César Teodoro Mendes, Weizhu Chen, Vishrav Chaudhary, Parul Chopra, Allie Del Giorno, Gustavo de Rosa, Matthew Dixon, Ronen Eldan, Dan Iter, Amit Garg, Abhishek Goswami, Suriya Gunasekar, Emman Haider, Junheng Hao, Russell J. Hewett, Jamie Huynh, Mojan Javaheripi, Xin Jin, Piero Kauffmann, Nikos Karampatziakis, Dongwoo Kim, Mahoud Khademi, Lev Kurilenko, James R. Lee, Yin Tat Lee, Yuanzhi Li, Chen Liang, Weishung Liu, Eric Lin, Zeqi Lin, Piyush Madan, Arindam Mitra, Hardik Modi, Anh Nguyen, Brandon Norick, Barun Patra, Daniel Perez-Becker, Thomas Portet, Reid Pryzant, Heyang Qin, Marko Radmilac, Corby Rosset, Sambudha Roy, Olatunji Ruwase, Olli Saarikivi, Amin Saied, Adil Salim, Michael Santacroce, Shital Shah, Ning Shang, Hiteshi Sharma, Xia Song, Masahiro Tanaka, Xin Wang, Rachel Ward, Guanhua Wang, Philipp Witte, Michael Wyatt, Can Xu, Jiahang Xu, Sonali Yadav, Fan Yang, Ziyi Yang, Donghan Yu, Chengruidong Zhang, Cyril Zhang, Jianwen Zhang, Li Lyna Zhang, Yi Zhang, Yue Zhang, Yunan Zhang, Xiren Zhou.

src/models.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5973,14 +5973,32 @@ export class DecisionTransformerModel extends DecisionTransformerPreTrainedModel
59735973

59745974
//////////////////////////////////////////////////
59755975
// PatchTST Transformer models
5976-
59775976
export class PatchTSTPreTrainedModel extends PreTrainedModel { }
59785977

5978+
/**
5979+
* The bare PatchTST Model outputting raw hidden-states without any specific head.
5980+
*/
5981+
export class PatchTSTModel extends PatchTSTPreTrainedModel { }
5982+
59795983
/**
59805984
* The PatchTST for prediction model.
59815985
*/
59825986
export class PatchTSTForPrediction extends PatchTSTPreTrainedModel { }
5987+
//////////////////////////////////////////////////
5988+
5989+
//////////////////////////////////////////////////
5990+
// PatchTSMixer Transformer models
5991+
export class PatchTSMixerPreTrainedModel extends PreTrainedModel { }
5992+
5993+
/**
5994+
* The bare PatchTSMixer Model outputting raw hidden-states without any specific head.
5995+
*/
5996+
export class PatchTSMixerModel extends PatchTSMixerPreTrainedModel { }
59835997

5998+
/**
5999+
* The PatchTSMixer for prediction model.
6000+
*/
6001+
export class PatchTSMixerForPrediction extends PatchTSMixerPreTrainedModel { }
59846002
//////////////////////////////////////////////////
59856003

59866004

@@ -6122,7 +6140,8 @@ const MODEL_MAPPING_NAMES_ENCODER_ONLY = new Map([
61226140
['efficientnet', ['EfficientNetModel', EfficientNetModel]],
61236141

61246142
['decision_transformer', ['DecisionTransformerModel', DecisionTransformerModel]],
6125-
['patchtst', ['PatchTSTForPrediction', PatchTSTForPrediction]],
6143+
['patchtst', ['PatchTSTForPrediction', PatchTSTModel]],
6144+
['patchtsmixer', ['PatchTSMixerForPrediction', PatchTSMixerModel]],
61266145

61276146
['mobilenet_v1', ['MobileNetV1Model', MobileNetV1Model]],
61286147
['mobilenet_v2', ['MobileNetV2Model', MobileNetV2Model]],
@@ -6407,6 +6426,11 @@ const MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES = new Map([
64076426
['vitmatte', ['VitMatteForImageMatting', VitMatteForImageMatting]],
64086427
]);
64096428

6429+
const MODEL_FOR_TIME_SERIES_PREDICTION_MAPPING_NAMES = new Map([
6430+
['patchtst', ['PatchTSTForPrediction', PatchTSTForPrediction]],
6431+
['patchtsmixer', ['PatchTSMixerForPrediction', PatchTSMixerForPrediction]],
6432+
])
6433+
64106434
const MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES = new Map([
64116435
['swin2sr', ['Swin2SRForImageSuperResolution', Swin2SRForImageSuperResolution]],
64126436
])
@@ -6448,6 +6472,7 @@ const MODEL_CLASS_TYPE_MAPPING = [
64486472
[MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly],
64496473
[MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly],
64506474
[MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES, MODEL_TYPES.EncoderOnly],
6475+
[MODEL_FOR_TIME_SERIES_PREDICTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly],
64516476
[MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES, MODEL_TYPES.EncoderOnly],
64526477
[MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly],
64536478
[MODEL_FOR_NORMAL_ESTIMATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly],

tests/tiny_random.test.js

Lines changed: 111 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ import {
5353
VisionEncoderDecoderModel,
5454
Florence2ForConditionalGeneration,
5555
MarianMTModel,
56+
PatchTSTModel,
57+
PatchTSTForPrediction,
58+
PatchTSMixerModel,
59+
PatchTSMixerForPrediction,
5660

5761
// Pipelines
5862
pipeline,
@@ -1839,14 +1843,119 @@ describe("Tiny random models", () => {
18391843
});
18401844
});
18411845

1846+
describe("patchtsmixer", () => {
1847+
const dims = [64, 512, 7];
1848+
const prod = dims.reduce((a, b) => a * b, 1);
1849+
const past_values = new Tensor(
1850+
"float32",
1851+
Float32Array.from({ length: prod }, (_, i) => i / prod),
1852+
dims,
1853+
);
1854+
1855+
describe("PatchTSMixerModel", () => {
1856+
const model_id = "hf-internal-testing/tiny-random-PatchTSMixerModel";
1857+
1858+
/** @type {PatchTSMixerModel} */
1859+
let model;
1860+
beforeAll(async () => {
1861+
model = await PatchTSMixerModel.from_pretrained(model_id, {
1862+
// TODO move to config
1863+
...DEFAULT_MODEL_OPTIONS,
1864+
});
1865+
}, MAX_MODEL_LOAD_TIME);
1866+
1867+
it(
1868+
"default",
1869+
async () => {
1870+
const { last_hidden_state } = await model({ past_values });
1871+
1872+
const { num_input_channels, num_patches, d_model } = model.config;
1873+
expect(last_hidden_state.dims).toEqual([dims[0], num_input_channels, num_patches, d_model]);
1874+
expect(last_hidden_state.mean().item()).toBeCloseTo(0.03344963490962982, 5);
1875+
},
1876+
MAX_TEST_EXECUTION_TIME,
1877+
);
1878+
1879+
afterAll(async () => {
1880+
await model?.dispose();
1881+
}, MAX_MODEL_DISPOSE_TIME);
1882+
});
1883+
1884+
describe("PatchTSMixerForPrediction", () => {
1885+
const model_id = "onnx-community/granite-timeseries-patchtsmixer";
1886+
1887+
/** @type {PatchTSMixerForPrediction} */
1888+
let model;
1889+
beforeAll(async () => {
1890+
model = await PatchTSMixerForPrediction.from_pretrained(model_id, {
1891+
// TODO move to config
1892+
...DEFAULT_MODEL_OPTIONS,
1893+
});
1894+
}, MAX_MODEL_LOAD_TIME);
1895+
1896+
it(
1897+
"default",
1898+
async () => {
1899+
const { prediction_outputs } = await model({ past_values });
1900+
1901+
const { prediction_length, num_input_channels } = model.config;
1902+
expect(prediction_outputs.dims).toEqual([dims[0], prediction_length, num_input_channels]);
1903+
expect(prediction_outputs.mean().item()).toBeCloseTo(0.5064773559570312, 5);
1904+
},
1905+
MAX_TEST_EXECUTION_TIME,
1906+
);
1907+
1908+
afterAll(async () => {
1909+
await model?.dispose();
1910+
}, MAX_MODEL_DISPOSE_TIME);
1911+
});
1912+
});
1913+
18421914
describe("patchtst", () => {
1915+
const dims = [64, 512, 7];
1916+
const prod = dims.reduce((a, b) => a * b, 1);
1917+
const past_values = new Tensor(
1918+
"float32",
1919+
Float32Array.from({ length: prod }, (_, i) => i / prod),
1920+
dims,
1921+
);
1922+
1923+
describe("PatchTSTModel", () => {
1924+
const model_id = "hf-internal-testing/tiny-random-PatchTSTModel";
1925+
1926+
/** @type {PatchTSTModel} */
1927+
let model;
1928+
beforeAll(async () => {
1929+
model = await PatchTSTModel.from_pretrained(model_id, {
1930+
// TODO move to config
1931+
...DEFAULT_MODEL_OPTIONS,
1932+
});
1933+
}, MAX_MODEL_LOAD_TIME);
1934+
1935+
it(
1936+
"default",
1937+
async () => {
1938+
const { last_hidden_state } = await model({ past_values });
1939+
1940+
const { num_input_channels, d_model } = model.config;
1941+
expect(last_hidden_state.dims).toEqual([dims[0], num_input_channels, 43, d_model]);
1942+
expect(last_hidden_state.mean().item()).toBeCloseTo(0.016672514379024506, 5);
1943+
},
1944+
MAX_TEST_EXECUTION_TIME,
1945+
);
1946+
1947+
afterAll(async () => {
1948+
await model?.dispose();
1949+
}, MAX_MODEL_DISPOSE_TIME);
1950+
});
1951+
18431952
describe("PatchTSTForPrediction", () => {
1844-
const model_id = "onnx-community/test-patchtst";
1953+
const model_id = "onnx-community/granite-timeseries-patchtst";
18451954

18461955
/** @type {PatchTSTForPrediction} */
18471956
let model;
18481957
beforeAll(async () => {
1849-
model = await BertModel.from_pretrained(model_id, {
1958+
model = await PatchTSTForPrediction.from_pretrained(model_id, {
18501959
// TODO move to config
18511960
...DEFAULT_MODEL_OPTIONS,
18521961
});
@@ -1855,13 +1964,6 @@ describe("Tiny random models", () => {
18551964
it(
18561965
"default",
18571966
async () => {
1858-
const dims = [64, 512, 7];
1859-
const prod = dims.reduce((a, b) => a * b, 1);
1860-
const past_values = new Tensor(
1861-
"float32",
1862-
Float32Array.from({ length: prod }, (_, i) => i / prod),
1863-
dims,
1864-
);
18651967
const { prediction_outputs } = await model({ past_values });
18661968

18671969
const { prediction_length, num_input_channels } = model.config;

0 commit comments

Comments
 (0)