Skip to content

Commit a317dca

Browse files
authored
Merge pull request #15 from matlab-deep-learning/compatibility
Add compatibility code for gpt2
2 parents 32cad3b + f9433b3 commit a317dca

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

Diff for: +gpt2/+internal/getSupportFilePath.m

+37
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
arguments
88
fileName (1,1) string
99
end
10+
if ismember(version('-release'),["2020a","2020b"])
11+
filePath = legacySupportFilePath(fileName);
12+
return
13+
end
1014
sd = matlab.internal.examples.utils.getSupportFileDir();
1115
localFileDir = {"data","networks"};%#ok
1216
localFile = fullfile(sd,"nnet",localFileDir{:},fileName);
@@ -15,4 +19,37 @@
1519
end
1620
fileURL = strjoin([localFileDir,fileName],"/");
1721
filePath = matlab.internal.examples.downloadSupportFile("nnet",fileURL);
22+
end
23+
24+
function filePath = legacySupportFilePath(fileName)
25+
% For releases before matlab.internal.examples.downloadSupportFile,
26+
% use manual download code. We save to the repo's root directory instead of
27+
% the userpath.
28+
% Create directories for the model.
29+
modelType = 'gpt2-355M';
30+
modelDirectory = fullfile(fileparts(mfilename('fullpath')),'..','..',modelType);
31+
filePath = fullfile(modelDirectory,fileName);
32+
iCreateDirectoryIfItDoesNotExist(modelDirectory);
33+
34+
iDownloadFileIfItDoesNotExist( ...
35+
filePath, ...
36+
"https://ssd.mathworks.com/supportfiles/nnet/data/networks/"+fileName );
37+
end
38+
39+
function iCreateDirectoryIfItDoesNotExist(directory)
40+
if ~exist(directory, 'dir')
41+
fprintf('Creating directory ''%s''...\n', directory);
42+
mkdir(directory);
43+
else
44+
fprintf('Skipped creating directory ''%s'' as it already exists\n', directory);
45+
end
46+
end
47+
48+
function iDownloadFileIfItDoesNotExist(destination, source)
49+
if ~exist(destination, 'file')
50+
fprintf('Downloading file ''%s'' ...\n', destination);
51+
websave(destination, source);
52+
else
53+
fprintf('Skipped downloading file ''%s'' as it already exists\n', destination);
54+
end
1855
end

Diff for: README.md

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ This repository implements deep learning transformer models in MATLAB.
1515
## Getting Started
1616
Download or [clone](https://www.mathworks.com/help/matlab/matlab_prog/use-source-control-with-projects.html#mw_4cc18625-9e78-4586-9cc4-66e191ae1c2c) this repository to your machine and open it in MATLAB.
1717

18-
For R2020a or R2020b download the repo at the corresponding tagged release, or if you are using `git` then `git checkout <release>` where `<release>` is `R2020a` or `R2020b`.
19-
2018
## Functions
2119
### bert
2220
`mdl = bert` loads a pretrained BERT transformer model and if necessary, downloads the model weights. The output `mdl` is structure with fields `Tokenizer` and `Parameters` that contain the BERT tokenizer and the model parameters, respectively.

0 commit comments

Comments
 (0)