Skip to content

Commit 0aed90b

Browse files
should make models directory if it does not exist
1 parent 9acf9f7 commit 0aed90b

1 file changed

Lines changed: 88 additions & 31 deletions

File tree

synapse/cli/deploy_model.py

Lines changed: 88 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import synapse.client.sftp as sftp
1212
from synapse.cli.files import setup_connection
13-
from synapse.utils.model_converter import convert_to_dlc
1413

1514
# Constants
1615
DEVICE_MODEL_DIR = "/models"
@@ -174,31 +173,64 @@ def deploy_model(args):
174173
if quantize:
175174
fmt_str = "Quantized DLC (INT8) — runs on DSP"
176175
else:
177-
fmt_str = "Float DLC — runs on CPU/GPU"
176+
fmt_str = "ONNX (float32) — runs on CPU via ONNX Runtime"
178177

179178
console.print(f"[bold]Deploying model:[/bold] {model_name}")
180179
console.print(f"[bold]Source:[/bold] {args.model_path}")
181180
console.print(f"[bold]Format:[/bold] {fmt_str}")
182181
console.print()
183182

184-
# Step 1: Convert model
183+
# Step 1: Prepare model for deployment
185184
if quantize:
185+
# Quantized path: convert to DLC via Docker (requires QAIRT SDK)
186186
console.print("[bold cyan]Converting model to quantized DLC...[/bold cyan]")
187+
188+
from synapse.utils.model_converter import convert_to_dlc
189+
190+
dlc_path = convert_to_dlc(
191+
args.model_path,
192+
input_shape=input_shape,
193+
snpe_root=args.snpe_root,
194+
quantize=quantize,
195+
input_list=args.input_list,
196+
console=console,
197+
)
198+
199+
if dlc_path is None:
200+
console.print("[bold red]Model conversion failed[/bold red]")
201+
return
202+
203+
deploy_path = dlc_path
204+
remote_ext = ".dlc"
187205
else:
188-
console.print("[bold cyan]Converting model to DLC...[/bold cyan]")
189-
190-
dlc_path = convert_to_dlc(
191-
args.model_path,
192-
input_shape=input_shape,
193-
snpe_root=args.snpe_root,
194-
quantize=quantize,
195-
input_list=args.input_list,
196-
console=console,
197-
)
206+
# Non-quantized path: deploy .onnx directly (no QAIRT SDK needed)
207+
ext = os.path.splitext(args.model_path)[1].lower()
198208

199-
if dlc_path is None:
200-
console.print("[bold red]Model conversion failed[/bold red]")
201-
return
209+
if ext == ".dlc":
210+
deploy_path = args.model_path
211+
remote_ext = ".dlc"
212+
elif ext == ".pt":
213+
console.print("[bold cyan]Converting PyTorch model to ONNX...[/bold cyan]")
214+
from synapse.utils.model_converter.pt_to_onnx import convert_pt_to_onnx
215+
216+
onnx_path = convert_pt_to_onnx(
217+
args.model_path,
218+
input_shape=input_shape,
219+
console=console,
220+
)
221+
if onnx_path is None:
222+
console.print("[bold red]Model conversion failed[/bold red]")
223+
return
224+
225+
deploy_path = onnx_path
226+
remote_ext = ".onnx"
227+
elif ext == ".onnx":
228+
deploy_path = args.model_path
229+
remote_ext = ".onnx"
230+
else:
231+
console.print(f"[bold red]Error:[/bold red] Unsupported file type: {ext}")
232+
console.print("[yellow]Supported formats: .pt, .onnx, .dlc[/yellow]")
233+
return
202234

203235
console.print()
204236

@@ -220,15 +252,15 @@ def deploy_model(args):
220252

221253
try:
222254
# Step 3: Ensure model directory exists
223-
_ensure_model_dir(sftp_conn, console)
255+
_ensure_model_dir(sftp_conn, ssh, console)
224256

225257
# Step 4: Check if model already exists on device
226-
remote_path = f"{DEVICE_MODEL_DIR}/{model_name}.dlc"
258+
remote_path = f"{DEVICE_MODEL_DIR}/{model_name}{remote_ext}"
227259
try:
228260
sftp_conn.stat(remote_path)
229261
if not args.force:
230262
if not Confirm.ask(
231-
f"[yellow]Model '{model_name}.dlc' already exists on device. Overwrite?[/yellow]",
263+
f"[yellow]Model '{model_name}{remote_ext}' already exists on device. Overwrite?[/yellow]",
232264
default=False,
233265
):
234266
console.print("[dim]Aborted.[/dim]")
@@ -237,16 +269,16 @@ def deploy_model(args):
237269
pass
238270

239271
# Step 5: Upload the model file
240-
_upload_file(sftp_conn, dlc_path, remote_path, console)
272+
_upload_file(sftp_conn, deploy_path, remote_path, console)
241273

242274
console.print()
243275
console.print("[bold green]Model deployed successfully![/bold green]")
244276
console.print()
245-
console.print(f" Model deployed: [cyan]models/{model_name}.dlc[/cyan]")
277+
console.print(f" Model deployed: [cyan]models/{model_name}{remote_ext}[/cyan]")
246278
if quantize:
247279
console.print(f" Runtime: [cyan]DSP (quantized INT8)[/cyan]")
248280
else:
249-
console.print(f" Runtime: [cyan]CPU (float32)[/cyan]")
281+
console.print(f" Runtime: [cyan]CPU (float32, ONNX Runtime)[/cyan]")
250282
console.print()
251283
console.print(
252284
" [dim]Tip: for faster DSP inference (~1ms), redeploy with --quantize --input-list[/dim]"
@@ -259,18 +291,43 @@ def deploy_model(args):
259291
sftp.close_sftp(ssh, sftp_conn)
260292

261293

262-
def _ensure_model_dir(sftp_conn, console: Console):
263-
"""Ensure the model directory exists on the device."""
294+
def _ensure_model_dir(sftp_conn, ssh, console: Console):
295+
"""Ensure the model directory exists on the device.
296+
297+
Tries SFTP mkdir first. If that fails (permission denied in chroot),
298+
falls back to SSH exec to create and chown the directory.
299+
"""
264300
try:
265301
sftp_conn.stat(DEVICE_MODEL_DIR)
302+
return
266303
except FileNotFoundError:
267-
console.print(f"[blue]Creating model directory: {DEVICE_MODEL_DIR}[/blue]")
268-
try:
269-
sftp_conn.mkdir(DEVICE_MODEL_DIR)
270-
except Exception as e:
271-
console.print(
272-
f"[yellow]Warning: Could not create model directory: {e}[/yellow]"
273-
)
304+
pass
305+
306+
console.print(f"[blue]Creating model directory: {DEVICE_MODEL_DIR}[/blue]")
307+
308+
# Try SFTP mkdir first
309+
try:
310+
sftp_conn.mkdir(DEVICE_MODEL_DIR)
311+
return
312+
except Exception:
313+
pass
314+
315+
# SFTP failed — try SSH command to create with proper ownership
316+
real_path = f"/opt/scifi/data{DEVICE_MODEL_DIR}"
317+
try:
318+
_, stdout, stderr = ssh.exec_command(
319+
f"mkdir -p {real_path} && chown $(whoami) {real_path}"
320+
)
321+
exit_code = stdout.channel.recv_exit_status()
322+
if exit_code == 0:
323+
console.print(f"[green]Created {DEVICE_MODEL_DIR}[/green]")
324+
return
325+
except Exception:
326+
pass
327+
328+
console.print(
329+
f"[bold red]Error: Could not create model directory on device.[/bold red]"
330+
)
274331

275332

276333
def _upload_file(sftp_conn, local_path: str, remote_path: str, console: Console):

0 commit comments

Comments
 (0)