Update Makefile for macOS Clang + OpenMP + C++11#2
Conversation
|
I unintentionally closed the pull request! |
|
I closed this by mistake then reopened it. |
Thank you for pr and I have merged it. I am working on a new design at here. After I finish my work I will publish it here. I hope you will like the new version. |
|
The makefile_osx works for MacOs so that is fine.
I was wondering if you have a decoder.cpp which I could use in a language
model?
Thank you.
Ralph
…On Tue, May 27, 2025 at 6:03 AM freelw ***@***.***> wrote:
*freelw* left a comment (freelw/cpp-transformer#2)
<#2 (comment)>
I found new makefile does not work on x64 linux. I revert it and add a new
makefile_osx.
#3
Can you test whether makefile_osx works on your MacBook? By this cmd :
make -f makefile_osx
> 2025年5月27日 16:02,Ralph Dratman ***@***.***> 写道:
>
> Dratman
—
Reply to this email directly, view it on GitHub
<#2 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAWZPUD4YRBZ4F63OLKJTT3AQZ6TAVCNFSM6AAAAAB57IZHM2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSMJRHEZTMMRRGE>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
|
I am interested in a C++ small language model with no GPU code for
experimentation and study. I would pay something for you to build that for
me.
On Thu, Jun 5, 2025 at 2:06 PM Ralph Dratman ***@***.***>
wrote:
… The makefile_osx works for MacOs so that is fine.
I was wondering if you have a decoder.cpp which I could use in a language
model?
Thank you.
Ralph
On Tue, May 27, 2025 at 6:03 AM freelw ***@***.***> wrote:
> *freelw* left a comment (freelw/cpp-transformer#2)
> <#2 (comment)>
> I found new makefile does not work on x64 linux. I revert it and add a
> new makefile_osx.
> #3
> Can you test whether makefile_osx works on your MacBook? By this cmd :
> make -f makefile_osx
>
> > 2025年5月27日 16:02,Ralph Dratman ***@***.***> 写道:
> >
> > Dratman
>
> —
> Reply to this email directly, view it on GitHub
> <#2 (comment)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AAAWZPUD4YRBZ4F63OLKJTT3AQZ6TAVCNFSM6AAAAAB57IZHM2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSMJRHEZTMMRRGE>
> .
> You are receiving this because you modified the open/close state.Message
> ID: ***@***.***>
>
|
I am currently learning GPT-2 and will implement a version of it. By then, there might be the decoder.cpp you want. |
|
GPT-2 would be great.
I can make a financial contribution if that would be helpful.
Meanwhile I transferred your encoder-only project in the XCode IDE on
MacOS, building in that environment and soon I think it will be fully
running in XCode.
(Previously I had just used the MacOS terminal to get the encoder-only
project working, creating the same output as yours. XCode is just an
alternate way to produce the same results.)
Thanks!
Ralph
…On Fri, Jun 6, 2025 at 11:17 PM freelw ***@***.***> wrote:
*freelw* left a comment (freelw/cpp-transformer#2)
<#2 (comment)>
The makefile_osx works for MacOs so that is fine. I was wondering if you
have a decoder.cpp which I could use in a language model? Thank you. Ralph
… <#m_1281260957338099353_>
On Tue, May 27, 2025 at 6:03 AM freelw *@*.*> wrote: freelw left a
comment (freelw/cpp-transformer#2
<#2>) <#2 (comment)
<#2 (comment)>>
I found new makefile does not work on x64 linux. I revert it and add a new
makefile_osx. #3 <#3> Can
you test whether makefile_osx works on your MacBook? By this cmd : make -f
makefile_osx > 2025年5月27日 16:02,Ralph Dratman @.*> 写道: > > Dratman —
Reply to this email directly, view it on GitHub <#2 (comment)
<#2 (comment)>>,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAAWZPUD4YRBZ4F63OLKJTT3AQZ6TAVCNFSM6AAAAAB57IZHM2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSMJRHEZTMMRRGE
. You are receiving this because you modified the open/close state.Message
ID: *@*.***>
I am currently learning GPT-2 and will implement a version of it. By then,
there might be the decoder.cpp you want.
—
Reply to this email directly, view it on GitHub
<#2 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAWZPXAFU55QH57Z4PLBQL3CJKTRAVCNFSM6AAAAAB57IZHM2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSNJRGU3TAOJZGA>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
#23 This PR has merged the language model functionality. |
you can use this script ./build_mac_cpu.sh to build on your macbook. |
|
Thank you very much for creating lm.cpp and for the makefile changes! Indeed, that is helpful. 谢谢你 |
You're welcome. Thank you too for replying in Chinese. |
checkpoint_20250609_123600_7.bin
|
|
Thanks very much again. I will try that checkpoint after some sleep! I assume you must occasionally sleep too. |
|
There is some problem with the link for
https://cpp-transformer-1252366230.cos.ap-beijing.myqcloud.com/lm/checkpoint_20250609_123600_7.bin
i tried several different ways but cannot download the file.
Could you try attaching that to an email reply? Thank you. Ralph
…On Sun, Jun 8, 2025 at 8:19 AM freelw ***@***.***> wrote:
*freelw* left a comment (freelw/cpp-transformer#2)
<#2 (comment)>
GPT-2 would be great. I can make a financial contribution if that would be
helpful. Meanwhile I transferred your encoder-only project in the XCode IDE
on MacOS, building in that environment and soon I think it will be fully
running in XCode. (Previously I had just used the MacOS terminal to get the
encoder-only project working, creating the same output as yours. XCode is
just an alternate way to produce the same results.) Thanks! Ralph
… <#m_-5458829483201424775_>
On Fri, Jun 6, 2025 at 11:17 PM freelw *@*.*> wrote: freelw left a
comment (freelw/cpp-transformer#2
<#2>) <#2 (comment)
<#2 (comment)>>
The makefile_osx works for MacOs so that is fine. I was wondering if you
have a decoder.cpp which I could use in a language model? Thank you. Ralph
… <#m_1281260957338099353_> On Tue, May 27, 2025 at 6:03 AM freelw @.>
wrote: freelw left a comment (freelw/cpp-transformer#2
<#2> <#2
<#2>>) <#2
<#2> (comment) <#2 (comment)
<#2 (comment)>>>
I found new makefile does not work on x64 linux. I revert it and add a new
makefile_osx. #3 <#3> <#3
<#3>> Can you test whether
makefile_osx works on your MacBook? By this cmd : make -f makefile_osx >
2025年5月27日 16:02,Ralph Dratman @.> 写道: > > Dratman — Reply to this email
directly, view it on GitHub <#2
<#2> (comment) <#2 (comment)
<#2 (comment)>>>,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAAWZPUD4YRBZ4F63OLKJTT3AQZ6TAVCNFSM6AAAAAB57IZHM2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSMJRHEZTMMRRGE
<https://github.com/notifications/unsubscribe-auth/AAAWZPUD4YRBZ4F63OLKJTT3AQZ6TAVCNFSM6AAAAAB57IZHM2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSMJRHEZTMMRRGE>
. You are receiving this because you modified the open/close state.Message
ID: @.**> I am currently learning GPT-2 and will implement a version of
it. By then, there might be the decoder.cpp you want. — Reply to this email
directly, view it on GitHub <#2 (comment)
<#2 (comment)>>,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAAWZPXAFU55QH57Z4PLBQL3CJKTRAVCNFSM6AAAAAB57IZHM2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSNJRGU3TAOJZGA
<https://github.com/notifications/unsubscribe-auth/AAAWZPXAFU55QH57Z4PLBQL3CJKTRAVCNFSM6AAAAAB57IZHM2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSNJRGU3TAOJZGA>
. You are receiving this because you modified the open/close state.Message
ID: @.**>
you can use this script ./build_mac_cpu.sh to build on your macbook.
—
Reply to this email directly, view it on GitHub
<#2 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAWZPQLENQJZDZ7UBPXYBL3CQS5BAVCNFSM6AAAAAB57IZHM2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSNJUGAZDANZRGU>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
us link |
|
Yes -- that download worked perfectly, and generated text. |
|
I will try training the lm on some other text. (I sent it a Quit signal from the Mac Task Manager, but it did not output a checkpoint on exit) |
let me test on my macbook |
I have found the cause of the problem. My program can only handle the Ctrl+C signal, not the quit signal. Moreover, after pressing Ctrl+C, it is necessary to wait until the batch training is completed before exiting normally. Do not force an early exit, as this is the only way to generate the checkpoint. |
|
Ah, that is helpful. I suggest you change the logic of the program so that it writes out a checkpoint after it completes each epoch. Then it would not be necessary to stop the program to get the checkpoint. |
In previous historical versions, it was indeed the case as you said, where a checkpoint was saved after each epoch. However, since each file was approximately 140MB, it filled up my hard drive, so I later changed it to the current setup. If you really need this feature, you can make minor modifications to lines 353 and 354 of lm.cpp, moving them up into the loop above. I don't think it should be too complicated. |
|
Recent output illustrating a few minor changes: |
|
Should the checkpoint file be correct at this point? Although there are bugs, it should be usable. Can you submit a PR? I'd like to review the code you modified. Additionally, I'm currently supporting Metal and it's estimated that the capability to invoke the GPU on a Mac will be available next week. Then training on a Mac should be faster. You can give it a try then. You can take a look at this branch. I'm currently implementing it.
|
|
Metal would be fantastic. I thought that would be very difficult and now I
am glad to know you do not think so!
I will send a PR, but not today because I am not sure of my changes yet.
…On Thu, Jun 12, 2025 at 9:07 AM freelw ***@***.***> wrote:
*freelw* left a comment (freelw/cpp-transformer#2)
<#2 (comment)>
Should the checkpoint file be correct at this point? Although there are
bugs, it should be usable. Can you submit a PR? I'd like to review the code
you modified.
Additionally, I'm currently supporting Metal and it's estimated that the
capability to invoke the GPU on a Mac will be available next week. Then
training on a Mac should be faster. You can give it a try then. You can
take a look at this branch.
<https://github.com/freelw/cpp-transformer/tree/wangli_dev_20250612> I'm
currently implementing it.
Recent output illustrating a few minor changes: --->./lm corpus :
./resources/time_machine/timemachine_preprocessed.txt epochs : 5 batch_size
: 16 gpu : 1 learning rate : 0.001 checkpoint to load from : max_words_cnt
: 256 token_ids_size : 256 Warning: GPU backend is not available in CPU
build. Now use cpu instead!!! Allocating memory for tensors : 36609236
bytes, for c_tensors: 3194706328 bytes for grad_tensors: 1241779004 bytes
epoch 0 : [224/256]loss : 5.59927 checkpoint saved :
./checkpoints/checkpoint_20250611_210257_-1.bin epoch 1 : [224/256]loss :
1.48668 checkpoint saved : ./checkpoints/checkpoint_20250612_041930_0.bin .
. . (still running)
—
Reply to this email directly, view it on GitHub
<#2 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAWZPVLRB2KL6T355I2A6L3DF3SFAVCNFSM6AAAAAB57IZHM2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSNRWGY2DKOBSGE>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
|
The language model is supposed to complete the "prompt" input, which here is called the "sentence" but I observe no change in the output in response to a new prompt. |
The result on my Ubuntu computer is as follows: there are two different sentences in ./test_lm.txt, and the output inference results are different.
|
I forgot to remind you of one thing. If you want to use other texts for training, you need to do some preprocessing and update the vocabulary, which means updating two files: 1 timemachine_preprocessed.txt, 2 vocab/time_machine/vocab.txt. The methods for preprocessing and constructing the vocabulary can be referred to here and here. |
#28 Cheer up! Metal is now supported! |
|
It takes about 4 hours to train one epoch on my MacBook Air M1.Using the complete data of the novel Time Machine, there are a total of 32,775 tokens. |
|
The speed is great
…On Sun, Jun 15, 2025 at 3:48 AM freelw ***@***.***> wrote:
*freelw* left a comment (freelw/cpp-transformer#2)
<#2 (comment)>
time ./lm -m 10000000 corpus :
./resources/time_machine/timemachine_preprocessed.txt epochs : 10
batch_size : 16 gpu : 1 learning rate : 0.001 checkpoint : max_words_cnt :
10000000 token_ids_size : 32775 Allocating memory for tensors : 36609236
bytes, for c_tensors: 3194706336 bytes for grad_tensors: 1241779004 bytes
epoch 0 : [32752/32775]loss : 8.3573 epoch 1 : [144/32775]loss : 8.35684
checkpoint saved : ./checkpoints/checkpoint_20250615_154537_2.bin ./lm -m
10000000 1157.25s user 2434.96s system 24% cpu 4:02:08.01 total
It takes about 4 hours to train one epoch on my MacBook Air M1.
—
Reply to this email directly, view it on GitHub
<#2 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAWZPX6N3TBM32CSX67PLD3DUQL5AVCNFSM6AAAAAB57IZHM2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSNZTGU3DONRUGA>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
|
But I must be doing something wrong because the loss is not going down much:
|
|
real 2m12.590s |
Have you made any changes to the code? If so, could you send me a pull request (PR) so that I can review it? |
|
Are you using the latest code on the main branch? |
you are right. there must be a bug. let me check |
I have now identified a phenomenon: when the allocated memory is too large, triggering swapout, the calculation results become incorrect. However, this issue does not occur in the CUDA environment because CUDA does not use unified memory and thus avoids swap-in/swap-out operations. The temporary solution I adopted was adjusting the default batchsize in lm.cpp, which has prevented swapout on my MacBook Air for now. This problem might stem from my insufficient understanding of the Metal API, but so far I haven’t found any standardized handling procedures for unified memory scenarios in the documentation. I will create an issue to track this problem going forward. |
You can try the latest mainline version again. The loss of the language model (LM) should converge normally now. |
|
Yes! The loss goes down very fast!
I continue testing in detail. Thanks for this!
Ralph
…On Mon, Jun 16, 2025 at 11:01 AM freelw ***@***.***> wrote:
*freelw* left a comment (freelw/cpp-transformer#2)
<#2 (comment)>
But I must be doing something wrong because the loss is not going down
much:
time ./lm
corpus : ./resources/time_machine/timemachine_preprocessed.txt
epochs : 10
batch_size : 16
gpu : 1
learning rate : 0.001
checkpoint :
max_words_cnt : 256
token_ids_size : 256
Allocating memory
for tensors : 36609236 bytes,
for c_tensors: 3194706336 bytes
for grad_tensors: 1241779004 bytes
epoch 0 : [224/256]loss : 8.46906
epoch 1 : [224/256]loss : 8.46934
epoch 2 : [224/256]loss : 8.47008
epoch 3 : [224/256]loss : 8.46854
epoch 4 : [224/256]loss : 8.46883
epoch 5 : [224/256]loss : 8.46955
epoch 6 : [224/256]loss : 8.46921
epoch 7 : [224/256]loss : 8.46919
epoch 8 : [224/256]loss : 8.46892
epoch 9 : [224/256]loss : 8.46953
checkpoint saved : ./checkpoints/checkpoint_20250615_235223_9.bin
You can try the latest mainline version again. The loss of the language
model (LM) should converge normally now.
I will conduct an experiment tomorrow to carefully compare the calculation
results under different environments in detail. By then, there should be
more clues to uncover.
—
Reply to this email directly, view it on GitHub
<#2 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAWZPUQKT6OCIRD34R62MT3D3L5PAVCNFSM6AAAAAB57IZHM2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSNZXGAYDKMJXGU>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
With some help from ChatGPT 4o I was able to build your cpp-transformer project on a MacBook M3.
This pull request modifies the makefile so that the project can successfully compile and run on macOS using Apple Clang.
My Linux system is down so I have not been able to check whether the modified makefile still works there.
Changes:
• Enable OpenMP support via Homebrew’s libomp
• Detect system SDK path using xcrun for compatibility with Clang
• Add -std=c++11 to support C++ features like override and auto
• Clean up compiler vs linker flags to avoid Clang warnings
Tested and confirmed working on:
• MacBook Pro (M3 Max), macOS 14.4.1
• Apple Clang + libomp
The training and inference output on the Mac matches the output shown in your Readme.
Thank you for making this project available.
Ralph Dratman
(https://github.com/dratman/cpp-transformer-rd)