Skip to content

Commit

Permalink
Use arm64 micromamba for Apple Silicon (#39)
Browse files Browse the repository at this point in the history
Issue:
It is best to use native architecture for the Apple Silicon if avialable.
For soma native binaries, e.g. pytorch, it is critical to use correct architecture, due to the use of architecture specific CPU extensions in the builds

Solution:
Extend download functions for mac to match CPU architecture of VSCode
Update tests to download x64 and arm64
Add downloaded binary architecture check
  • Loading branch information
anton-matosov committed Oct 8, 2023
1 parent 8375cc4 commit cd883c3
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ generated-icons/
*.vsix
tmp
.idea

/.micromamba/
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
"istanbul-lib-coverage": "^3.2.0",
"istanbul-lib-source-maps": "^4.0.1",
"jest": "^29.5.0",
"mock-os": "^1.0.0",
"prettier": "^2.8.6",
"rimraf": "^4.4.0",
"ts-jest": "^29.0.5",
Expand Down
35 changes: 34 additions & 1 deletion src/micromamba/downloadMicromamba.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as path from 'path'
import { spawnSync } from 'child_process'
import mockos from 'mock-os'
import { platform } from 'os'
import sh from '../sh'
import {
downloadMicromambaLinux,
Expand All @@ -10,9 +13,13 @@ const tmpDir = path.join(__dirname, 'tmp', path.basename(__filename))

describe('downloadMicromamba', () => {
beforeEach(async () => {
mockos.restore()
await sh.rmrf(tmpDir)
await sh.mkdirp(tmpDir)
})
afterEach(() => {
mockos.restore()
})

it('win32', async () => {
await downloadMicromambaWin(tmpDir)
Expand All @@ -26,9 +33,35 @@ describe('downloadMicromamba', () => {
expect(actual).toEqual(['micromamba'])
}, 10000)

it('darwin', async () => {
const getFileInfo = (filePath: string) => {
const res = spawnSync('file', [filePath], { encoding: 'utf8' })
expect(res.error).toBeUndefined()
expect(res.status).toEqual(0)
return res.stdout
}

it('darwin-x64', async () => {
mockos({ arch: 'x64' })
await downloadMicromambaMac(tmpDir)
const actual = await sh.ls(tmpDir)
expect(actual).toEqual(['micromamba'])

if (platform() == 'darwin') {
expect(getFileInfo(path.join(tmpDir, 'micromamba'))).toContain(
'Mach-O 64-bit executable x86_64',
)
}
}, 10000)

it('darwin-arm64', async () => {
mockos({ arch: 'arm64' })
await downloadMicromambaMac(tmpDir)
const actual = await sh.ls(tmpDir)
expect(actual).toEqual(['micromamba'])
if (platform() == 'darwin') {
expect(getFileInfo(path.join(tmpDir, 'micromamba'))).toContain(
'Mach-O 64-bit executable arm64',
)
}
}, 10000)
})
17 changes: 16 additions & 1 deletion src/micromamba/downloadMicromamba.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as tar from 'tar'
import bz2 from 'unbzip2-stream'
import { URL } from 'url'
import sh from '../sh'
import * as os from 'os'

export const _downloadMicromamba = async (url: string, tar: Writable): Promise<void> => {
try {
Expand Down Expand Up @@ -54,8 +55,22 @@ export const downloadMicromambaWin = async (cwd: string): Promise<void> => {
await _downloadMicromamba(url, stream)
}

const makeMacDownloadArch = (): string => {
const arch = os.arch()
switch (arch) {
case 'x64':
return '64'

case 'arm64':
return arch

default:
throw new Error(`${arch} CPU architecture is not supported by micromamba`)
}
}

export const downloadMicromambaMac = async (cwd: string): Promise<void> => {
const url = 'https://micromamba.snakepit.net/api/micromamba/osx-64/latest'
const url = `https://micromamba.snakepit.net/api/micromamba/osx-${makeMacDownloadArch()}/latest`
const stream = tar.x({ cwd, strip: 1 }, ['bin/micromamba'])
await _downloadMicromamba(url, stream)
await sh.chmodR('755', cwd)
Expand Down
1 change: 1 addition & 0 deletions tests/mock-os.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'mock-os'

0 comments on commit cd883c3

Please sign in to comment.