%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.11%22%0A%23%20dependencies%20%3D%20%5B%0A%23%20%20%20%20%20%22marimo%22%2C%0A%23%20%20%20%20%20%22numpy%22%2C%0A%23%20%20%20%20%20%22ml-dtypes%22%2C%0A%23%20%20%20%20%20%22zstandard%22%2C%0A%23%20%20%20%20%20%22matplotlib%22%2C%0A%23%20%5D%0A%23%20%2F%2F%2F%0A%22%22%22Pirate-vector%20activation%20steering%20%E2%80%94%20a%20worked%20end-to-end%20example.%0A%0ARuns%20entirely%20against%20the%20public%20ACS%20base-model%20API%20(no%20local%20GPU%2C%20no%20model%0Aweights)%3A%20harvest%20activations%2C%20build%20a%20steering%20vector%2C%20steer%2C%20measure.%0A%0A%20%20%20%20ACS_API_KEY%3D...%20uvx%20marimo%20edit%20--sandbox%20--watch%20pirate_steering.py%0A%0A(--watch%20keeps%20the%20browser%20session%20in%20sync%20if%20the%20file%20changes%20on%20disk.)%0AStatic%20export%20with%20outputs%3A%20%20uvx%20marimo%20export%20html%20pirate_steering.py%0A%22%22%22%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.23.13%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%20Steering%20a%20base%20model%20with%20a%20pirate%20vector%20%F0%9F%8F%B4%E2%80%8D%E2%98%A0%EF%B8%8F%0A%0A%20%20%20%20This%20notebook%20walks%20the%20**full%20activation-steering%20loop**%20against%20the%20ACS%0A%20%20%20%20base-model%20API%20%E2%80%94%20the%20same%20%60%2Fv1%2Fcompletions%60%20endpoint%20you%20already%20use%2C%20no%20local%0A%20%20%20%20GPU%20required%3A%0A%0A%20%20%20%201.%20**Harvest**%20residual-stream%20activations%20for%20pirate-styled%20vs.%20plain%20sentences%0A%20%20%20%20%20%20%20(%5Bharvesting%20tutorial%5D(https%3A%2F%2Finfra.acsresearch.org%2Ftutorial%2Factivation-harvesting))%3B%0A%20%20%20%202.%20build%20a%20**pirate%20direction**%20%E2%80%94%20the%20difference%20of%20means%20between%20the%20two%20sets%3B%0A%20%20%20%203.%20**steer**%20with%20it%20(%5Bsteering%20tutorial%5D(https%3A%2F%2Finfra.acsresearch.org%2Ftutorial%2Factivation-steering))%0A%20%20%20%20%20%20%20and%20watch%20a%20weather%20forecast%20turn%20into%20a%20sea%20shanty%3B%0A%20%20%20%204.%20**quantify**%20the%20effect%20%E2%80%94%20a%20pirate-word%20score%2C%20a%20logprob%20preference%20metric%2C%0A%20%20%20%20%20%20%20and%20the%20characteristic%20*inverted-U*%20(steer%20too%20hard%20and%20fluency%20collapses).%0A%0A%20%20%20%20At%20the%20end%3A%20a%20short%20note%20on%20a%20popular%20recipe%20that%20does%20**not**%20transfer%20well%20to%0A%20%20%20%20base%20models%2C%20so%20you%20don't%20spend%20an%20afternoon%20rediscovering%20that.%0A%0A%20%20%20%20You%20need%20an%20ACS%20API%20key%20in%20%60ACS_API_KEY%60%20and%20a%20model%20with%0A%20%20%20%20%60%22activations%22%3A%20true%60%20in%20%60GET%20%2Fv1%2Fmodels%60%20(we%20use%20%60llama-8b%60).%20The%20first%0A%20%20%20%20activation%20request%20cold-boots%20a%20dedicated%20engine%20(~2%20min)%3B%20the%20whole%20notebook%0A%20%20%20%20is%20~50%20small%20requests%20(a%20couple%20of%20minutes%20warm).%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20import%20base64%0A%20%20%20%20import%20json%0A%20%20%20%20import%20os%0A%20%20%20%20import%20time%0A%20%20%20%20import%20urllib.error%0A%20%20%20%20import%20urllib.request%0A%0A%20%20%20%20import%20ml_dtypes%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20zstandard%20as%20zstd%0A%0A%20%20%20%20%23%20Accept%20the%20base%20URL%20with%20or%20without%20the%20%2Fv1%20suffix%20(and%20note%3A%20marimo%0A%20%20%20%20%23%20auto-loads%20a%20.env%20from%20the%20working%20directory%2C%20which%20can%20override%20this).%0A%20%20%20%20BASE%20%3D%20os.environ.get(%22ACS_API_BASE%22%2C%20%22https%3A%2F%2Finfra.acsresearch.org%2Fv1%22).rstrip(%22%2F%22)%0A%20%20%20%20if%20not%20BASE.endswith(%22%2Fv1%22)%3A%0A%20%20%20%20%20%20%20%20BASE%20%2B%3D%20%22%2Fv1%22%0A%20%20%20%20KEY%20%3D%20os.environ.get(%22ACS_API_KEY%22%2C%20%22%22)%0A%20%20%20%20MODEL%20%3D%20%22llama-8b%22%0A%0A%20%20%20%20mo.stop(%0A%20%20%20%20%20%20%20%20not%20KEY%2C%0A%20%20%20%20%20%20%20%20mo.md(%22%E2%9A%A0%EF%B8%8F%20**Set%20%60ACS_API_KEY%60%20in%20your%20environment%20and%20re-run.**%22)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20def%20post(body%3A%20dict%2C%20timeout%3A%20int%20%3D%201200%2C%20retries%3A%20int%20%3D%208)%20-%3E%20dict%3A%0A%20%20%20%20%20%20%20%20%22%22%22POST%20%2Fv1%2Fcompletions%2C%20riding%20out%20cold%20boots%20and%20deploy%20blips.%0A%0A%20%20%20%20%20%20%20%20The%20activation%20engine%20scales%20to%20zero%20when%20idle%3B%20the%20first%20request%0A%20%20%20%20%20%20%20%20returns%20a%20retryable%20%60503%20modal_cold_boot%60.%20Brief%20404%2F502%2F503%20blips%0A%20%20%20%20%20%20%20%20(a%20deploy%20in%20flight)%20get%20a%20few%20quick%20retries%20too.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20req%20%3D%20urllib.request.Request(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7BBASE%7D%2Fcompletions%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20data%3Djson.dumps(body).encode()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20headers%3D%7B%22Authorization%22%3A%20f%22Bearer%20%7BKEY%7D%22%2C%20%22Content-Type%22%3A%20%22application%2Fjson%22%7D%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20transient_left%20%3D%203%0A%20%20%20%20%20%20%20%20for%20_%20in%20range(retries)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20json.load(urllib.request.urlopen(req%2C%20timeout%3Dtimeout))%0A%20%20%20%20%20%20%20%20%20%20%20%20except%20urllib.error.HTTPError%20as%20e%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20msg%20%3D%20e.read().decode()%5B%3A300%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20e.code%20%3D%3D%20503%20and%20%22cold_boot%22%20in%20msg%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20time.sleep(30)%20%20%23%20engine%20cold%20boot%3A%20be%20patient%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20e.code%20in%20(404%2C%20502%2C%20503)%20and%20transient_left%20%3E%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20transient_left%20-%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20time.sleep(10)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20raise%20RuntimeError(f%22HTTP%20%7Be.code%7D%20on%20%7Breq.full_url%7D%3A%20%7Bmsg%7D%22)%20from%20None%0A%20%20%20%20%20%20%20%20raise%20RuntimeError(f%22gave%20up%20waiting%20for%20the%20engine%20(%7Breq.full_url%7D)%22)%0A%0A%20%20%20%20mo.md(f%22Configured%20for%20**%60%7BMODEL%7D%60**%20at%20%60%7BBASE%7D%60%20%E2%80%94%20key%20found%20%E2%9C%93%22)%0A%20%20%20%20return%20MODEL%2C%20base64%2C%20ml_dtypes%2C%20np%2C%20post%2C%20zstd%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20The%20activation%20codec%0A%0A%20%20%20%20Both%20directions%20of%20the%20API%20use%20one%20tensor%20envelope%3A%20**bf16%20values%2C%20bit-cast%20to%0A%20%20%20%20int16%2C%20zstd-compressed%2C%20base64-encoded**.%20Two%20helpers%20straight%20from%20the%0A%20%20%20%20tutorial%20%E2%80%94%20decode%20what%20we%20harvest%2C%20encode%20what%20we%20inject%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(base64%2C%20ml_dtypes%2C%20np%2C%20zstd)%3A%0A%20%20%20%20def%20decode_residual_stream(resp%3A%20dict)%3A%0A%20%20%20%20%20%20%20%20%22%22%22activations.residual_stream%20-%3E%20np%20array%20(n_layers%2C%20n_tokens%2C%20hidden).%22%22%22%0A%20%20%20%20%20%20%20%20d%20%3D%20resp%5B%22activations%22%5D%5B%22residual_stream%22%5D%0A%20%20%20%20%20%20%20%20raw%20%3D%20base64.b64decode(d%5B%22data%22%5D)%0A%20%20%20%20%20%20%20%20if%20d.get(%22compression%22)%20%3D%3D%20%22zstd%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raw%20%3D%20zstd.ZstdDecompressor().decompress(raw)%0A%20%20%20%20%20%20%20%20arr%20%3D%20np.frombuffer(raw%2C%20dtype%3Dnp.uint16).view(ml_dtypes.bfloat16)%0A%20%20%20%20%20%20%20%20return%20arr.reshape(d%5B%22shape%22%5D)%0A%0A%20%20%20%20def%20encode_vector(vec)%20-%3E%20dict%3A%0A%20%20%20%20%20%20%20%20%22%22%22float%20array%20(n_layers%2C%20hidden)%20-%3E%20vLLM-Lens%20tensor%20codec%20dict.%22%22%22%0A%20%20%20%20%20%20%20%20a%20%3D%20vec.astype(ml_dtypes.bfloat16).view(np.int16)%0A%20%20%20%20%20%20%20%20packed%20%3D%20zstd.ZstdCompressor(level%3D1).compress(a.tobytes())%0A%20%20%20%20%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22data%22%3A%20base64.b64encode(packed).decode()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22dtype%22%3A%20%22int16%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22original_dtype%22%3A%20%22torch.bfloat16%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22shape%22%3A%20list(a.shape)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22compression%22%3A%20%22zstd%22%2C%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20return%20decode_residual_stream%2C%20encode_vector%0A%0A%0A%40app.cell%0Adef%20_(MODEL%2C%20decode_residual_stream%2C%20np%2C%20post)%3A%0A%20%20%20%20%23%20----%20experiment%20harness%20--------------------------------------------------%0A%0A%20%20%20%20def%20capture(prompt%3A%20str)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Full%20residual%20stream%20for%20a%20prompt%3A%20(n_layers%2C%20n_tokens%2C%20hidden)%20f32.%0A%0A%20%20%20%20%20%20%20%20Trust%20nothing%3A%20a%20failed%20capture%20hook%20is%20skipped%20server-side%20with%20only%20a%0A%20%20%20%20%20%20%20%20log%20warning%2C%20so%20assert%20the%20shape%20before%20using%20the%20data.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20r%20%3D%20post(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22model%22%3A%20MODEL%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22prompt%22%3A%20prompt%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22max_tokens%22%3A%201%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22temperature%22%3A%200%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22output_residual_stream%22%3A%20True%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20x%20%3D%20decode_residual_stream(r)%0A%20%20%20%20%20%20%20%20assert%20x.ndim%20%3D%3D%203%20and%20x.shape%5B0%5D%20%3D%3D%2032%2C%20f%22partial%20capture%3A%20%7Bx.shape%7D%22%0A%20%20%20%20%20%20%20%20x%20%3D%20x.astype(np.float32)%0A%20%20%20%20%20%20%20%20assert%20np.isfinite(x).all()%2C%20%22non-finite%20%E2%80%94%20broken%20capture%22%0A%20%20%20%20%20%20%20%20return%20x%0A%0A%20%20%20%20def%20gen(prompt%3A%20str%2C%20sv%3A%20dict%2C%20max_tokens%3A%20int%20%3D%2045)%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20%22%22%22One%20steered%20completion%20(temperature%200.8%2C%20pinned%20seed%20%E2%86%92%20reproducible).%0A%0A%20%20%20%20%20%20%20%20The%20vector%20is%20sent%20at%20EVERY%20strength%2C%20zero%20included%20%E2%80%94%20a%20zero-scale%20steer%0A%20%20%20%20%20%20%20%20is%20a%20proven%20identity%20(see%20the%20sanity%20check)%20and%20it%20keeps%20every%20data%0A%20%20%20%20%20%20%20%20point%20on%20the%20SAME%20engine%20(see%20the%20same-engine%20note%20below).%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20body%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22model%22%3A%20MODEL%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22prompt%22%3A%20prompt%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22max_tokens%22%3A%20max_tokens%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22temperature%22%3A%200.8%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22seed%22%3A%207%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22apply_steering_vectors%22%3A%20%5Bsv%5D%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20return%20post(body)%5B%22choices%22%5D%5B0%5D%5B%22text%22%5D%0A%0A%20%20%20%20PIRATE_WORDS%20%3D%20set(%0A%20%20%20%20%20%20%20%20%22arr%20arrr%20aye%20ye%20yer%20matey%20ahoy%20avast%20booty%20plunder%20treasure%20doubloon%20%22%0A%20%20%20%20%20%20%20%20%22scallywag%20scurvy%20landlubber%20buccaneer%20privateer%20sail%20sailed%20sailing%20sails%20%22%0A%20%20%20%20%20%20%20%20%22ship%20ships%20vessel%20schooner%20sloop%20frigate%20sea%20seas%20ocean%20waves%20captain%20%22%0A%20%20%20%20%20%20%20%20%22cap'n%20crew%20mateys%20grog%20rum%20anchor%20helm%20mast%20deck%20harbor%20port%20voyage%20%22%0A%20%20%20%20%20%20%20%20%22sailor%20pirate%20pirates%20parrot%20shanty%20seadog%20swab%20hearties%20yo-ho%20tis%22.split()%0A%20%20%20%20)%0A%0A%20%20%20%20def%20pirate_score(text%3A%20str)%20-%3E%20int%3A%0A%20%20%20%20%20%20%20%20%22%22%22Crude%20but%20legible%3A%20how%20many%20pirate%2Fnautical%20words%20in%20the%20text.%22%22%22%0A%20%20%20%20%20%20%20%20return%20sum(t.strip(%22.%2C!%3F%3B%3A'%5C%22%E2%80%94%22).lower()%20in%20PIRATE_WORDS%20for%20t%20in%20text.split())%0A%0A%20%20%20%20return%20capture%2C%20gen%2C%20pirate_score%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Build%20the%20pirate%20direction%0A%0A%20%20%20%20Six%20pirate-*styled*%20sentences%20vs.%20six%20semantically-matched%20plain%20paraphrases%20%E2%80%94%0A%20%20%20%20we%20contrast%20**actual%20text%20in%20the%20target%20style**%2C%20which%20is%20the%20design%20that%20works%0A%20%20%20%20on%20a%20base%20model%20(see%20the%20note%20at%20the%20end%20for%20what%20doesn't).%20Each%20sentence's%0A%20%20%20%20residuals%20are%20averaged%20over%20its%20content%20tokens%2C%20**skipping%20position%200**%3A%20the%0A%20%20%20%20BOS%20token%20is%20an%20attention%20sink%20with%20a%20huge%2C%20style-irrelevant%20norm.%20One%20capture%0A%20%20%20%20per%20sentence%20returns%20*all*%20layers%20at%20once%3B%20the%20direction%20is%20the%20difference%20of%0A%20%20%20%20means.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(capture%2C%20mo%2C%20np)%3A%0A%20%20%20%20PIRATE_SENTS%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%22Arr%2C%20matey%2C%20hand%20over%20the%20treasure!%22%2C%0A%20%20%20%20%20%20%20%20%22Ahoy%2C%20ye%20scurvy%20dogs%2C%20hoist%20the%20sails!%22%2C%0A%20%20%20%20%20%20%20%20%22Shiver%20me%20timbers%2C%20there%20be%20a%20storm%20brewin'!%22%2C%0A%20%20%20%20%20%20%20%20%22Avast%2C%20ye%20landlubbers%2C%20walk%20the%20plank!%22%2C%0A%20%20%20%20%20%20%20%20%22Yo%20ho%20ho%2C%20a%20pirate's%20life%20be%20grand!%22%2C%0A%20%20%20%20%20%20%20%20%22Batten%20down%20the%20hatches%2C%20me%20hearties!%22%2C%0A%20%20%20%20%5D%0A%20%20%20%20PLAIN_SENTS%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%22Please%20hand%20over%20the%20valuables.%22%2C%0A%20%20%20%20%20%20%20%20%22Hello%20everyone%2C%20raise%20the%20sails.%22%2C%0A%20%20%20%20%20%20%20%20%22Goodness%2C%20a%20storm%20is%20coming.%22%2C%0A%20%20%20%20%20%20%20%20%22Stop%2C%20you%20inexperienced%20people%2C%20leave%20the%20ship.%22%2C%0A%20%20%20%20%20%20%20%20%22Honestly%2C%20a%20sailor's%20life%20is%20great.%22%2C%0A%20%20%20%20%20%20%20%20%22Secure%20the%20ship%2C%20my%20friends.%22%2C%0A%20%20%20%20%5D%0A%0A%20%20%20%20def%20_mean_content(prompt)%3A%0A%20%20%20%20%20%20%20%20return%20capture(prompt)%5B%3A%2C%201%3A%2C%20%3A%5D.mean(axis%3D1)%20%20%23%20(32%2C%204096)%2C%20BOS%20excluded%0A%0A%20%20%20%20print(%22harvesting%2012%20sentences%E2%80%A6%22)%0A%20%20%20%20_pir%20%3D%20np.stack(%5B_mean_content(p)%20for%20p%20in%20PIRATE_SENTS%5D)%0A%20%20%20%20_pla%20%3D%20np.stack(%5B_mean_content(p)%20for%20p%20in%20PLAIN_SENTS%5D)%0A%20%20%20%20V%20%3D%20_pir.mean(axis%3D0)%20-%20_pla.mean(axis%3D0)%20%20%23%20(32%2C%204096)%3A%20per-layer%20direction%0A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22Pirate%20direction%20%60V%60%20built%3A%20shape%20%60%7BV.shape%7D%60%2C%20%22%0A%20%20%20%20%20%20%20%20f%22%E2%80%96V%E2%82%81%E2%82%86%E2%80%96%20%3D%20%7Bnp.linalg.norm(V%5B16%5D)%3A.2f%7D.%22%0A%20%20%20%20)%0A%20%20%20%20return%20(V%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Sanity%20check%20before%20steering%20anything%0A%0A%20%20%20%20A%20steering%20vector%20rides%20along%20in%20the%20completions%20request%20under%0A%20%20%20%20**%60apply_steering_vectors%60**.%20Before%20trusting%20any%20result%3A%20**%60scale%3A%200%60%20must%0A%20%20%20%20reproduce%20the%20baseline%20exactly**%20%E2%80%94%20if%20it%20doesn't%2C%20your%20request%20isn't%20reaching%0A%20%20%20%20the%20steering%20path%20at%20all.%0A%0A%20%20%20%20One%20subtlety%3A%20activation%20requests%20run%20on%20a%20**separate%20engine**%20from%20plain%0A%20%20%20%20completions%2C%20and%20two%20engines%20aren't%20bit-identical%20%E2%80%94%20even%20at%20%60temperature%3A%200%60%0A%20%20%20%20their%20outputs%20can%20differ%20by%20a%20token.%20So%20compare%20like%20with%20like%3A%20the%20baseline%0A%20%20%20%20below%20forces%20the%20activation%20engine%20too%20(via%20%60output_residual_stream%60)%2C%20making%0A%20%20%20%20the%20zero-scale%20check%20exact.%20For%20the%20same%20reason%2C%20every%20curve%20in%20this%20notebook%0A%20%20%20%20sends%20the%20vector%20at%20every%20strength%20%E2%80%94%20zero%20included.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(MODEL%2C%20V%2C%20encode_vector%2C%20mo%2C%20post)%3A%0A%20%20%20%20def%20pirate_sv(scale%3A%20float)%20-%3E%20dict%3A%0A%20%20%20%20%20%20%20%20%22%22%22Steering%20directive%3A%20layer%2016%20(justified%20by%20the%20sweep%20below)%2C%20raw%20scale.%22%22%22%0A%20%20%20%20%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22activations%22%3A%20encode_vector(V%5B16%5D.reshape(1%2C%20-1))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22layer_indices%22%3A%20%5B16%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22scale%22%3A%20float(scale)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22norm_match%22%3A%20False%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22position_indices%22%3A%20None%2C%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20_gen0%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22model%22%3A%20MODEL%2C%0A%20%20%20%20%20%20%20%20%22prompt%22%3A%20'I%20looked%20out%20at%20the%20sea%2C%20turned%20to%20the%20crew%2C%20and%20said%2C%20%22'%2C%0A%20%20%20%20%20%20%20%20%22max_tokens%22%3A%2045%2C%0A%20%20%20%20%20%20%20%20%22temperature%22%3A%200%2C%0A%20%20%20%20%20%20%20%20%23%20Force%20the%20activation%20engine%20for%20the%20baseline%20too%20(same-engine%20compare)%3A%0A%20%20%20%20%20%20%20%20%22output_residual_stream%22%3A%20True%2C%0A%20%20%20%20%7D%0A%20%20%20%20_baseline%20%3D%20post(_gen0)%5B%22choices%22%5D%5B0%5D%5B%22text%22%5D%0A%20%20%20%20_zero%20%3D%20post(%7B**_gen0%2C%20%22apply_steering_vectors%22%3A%20%5Bpirate_sv(0.0)%5D%7D)%5B%22choices%22%5D%5B0%5D%5B%22text%22%5D%0A%20%20%20%20assert%20_baseline%20%3D%3D%20_zero%2C%20%22scale-0%20steer%20must%20be%20the%20identity!%22%0A%0A%20%20%20%20mo.md(%22**Zero-scale%20identity%20holds**%20%E2%80%94%20the%20steering%20path%20is%20engaged%20and%20inert%20at%200.%20%E2%9C%93%22)%0A%20%20%20%20return%20(pirate_sv%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Which%20layer%3F%20Sweep%20it%0A%0A%20%20%20%20One%20good%20way%20to%20pick%20the%20layer%3A%20steer%20**each%20candidate%20layer%20alone**%20%E2%80%94%20same%0A%20%20%20%20scale%2C%20same%20seed%20%E2%80%94%20and%20read%20the%20outputs.%20Each%20layer%20is%20steered%20with%20its%20own%0A%20%20%20%20row%20of%20%60V%60%2C%20i.e.%20the%20direction%20*measured%20at%20that%20same%20layer*.%20Mid-stack%0A%20%20%20%20layers%20carry%20the%20style%3B%20very%20early%20layers%20are%20too%20token-bound%2C%20and%20late%0A%20%20%20%20layers%20too%20output-specific%2C%20to%20move%20much.%20That's%20why%20the%20rest%20of%20this%0A%20%20%20%20notebook%20fixes%20**layer%2016**.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(V%2C%20encode_vector%2C%20gen%2C%20mo%2C%20pirate_score)%3A%0A%20%20%20%20def%20_sv_at(layer%3A%20int%2C%20scale%3A%20float)%20-%3E%20dict%3A%0A%20%20%20%20%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22activations%22%3A%20encode_vector(V%5Blayer%5D.reshape(1%2C%20-1))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22layer_indices%22%3A%20%5Blayer%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22scale%22%3A%20float(scale)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22norm_match%22%3A%20False%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22position_indices%22%3A%20None%2C%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20_layer%20in%20%5B4%2C%208%2C%2012%2C%2016%2C%2020%2C%2024%2C%2028%5D%3A%0A%20%20%20%20%20%20%20%20_t%20%3D%20gen(%22The%20weather%20forecast%20for%20tomorrow%20says%22%2C%20_sv_at(_layer%2C%202.0))%0A%20%20%20%20%20%20%20%20_rows.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22layer%22%3A%20_layer%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%F0%9F%8F%B4%E2%80%8D%E2%98%A0%EF%B8%8F%22%3A%20pirate_score(_t)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22completion%20(scale%202.0)%22%3A%20_t.replace(%22%5Cn%22%2C%20%22%20%22)%5B%3A110%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20mo.ui.table(_rows%2C%20selection%3DNone)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Dose%E2%80%93response%0A%0A%20%20%20%20Same%20prompts%2C%20same%20seed%2C%20rising%20%60scale%60.%20Watch%20the%20completions%20go%20*normal%20%E2%86%92%0A%20%20%20%20pirate%20%E2%86%92%20heavy%20dialect%20%E2%86%92%20collapse*%3A%20the%20**inverted-U**%20that%20is%20the%20fundamental%0A%20%20%20%20trade-off%20of%20activation%20steering.%20The%20%60%F0%9F%8F%B4%E2%80%8D%E2%98%A0%EF%B8%8F%60%20column%20counts%20pirate%20words%20across%0A%20%20%20%20all%20three%20completions%20%E2%80%94%20and%20the%20third%20prompt%20(**job%20advice**%2C%20nothing%20nautical%0A%20%20%20%20about%20it)%20is%20the%20*generalization%20probe*%3A%20a%20direction%20that%20pirates%20*that*%20has%0A%20%20%20%20captured%20a%20concept%2C%20not%20just%20memorized%20our%20twelve%20harvest%20sentences.%0A%0A%20%20%20%20The%20**sweet%20spot**%20%E2%80%94%20the%20scale%20band%20where%20the%20style%20shift%20is%20strong%20but%0A%20%20%20%20fluency%20still%20intact%20%E2%80%94%20is%20found%20exactly%20this%20way%3A%20sweep%20and%20read.%20Here%20it%0A%20%20%20%20is%20%E2%89%88%20**1.5%E2%80%932.0**.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(gen%2C%20mo%2C%20pirate_score%2C%20pirate_sv)%3A%0A%20%20%20%20SCALES%20%3D%20%5B0.0%2C%201.5%2C%202.0%2C%202.5%2C%203.0%5D%0A%20%20%20%20PROMPT_SPEECH%20%3D%20'I%20looked%20out%20at%20the%20sea%2C%20turned%20to%20the%20crew%2C%20and%20said%2C%20%22'%0A%20%20%20%20PROMPT_WEATHER%20%3D%20%22The%20weather%20forecast%20for%20tomorrow%20says%22%0A%20%20%20%20PROMPT_JOB%20%3D%20%22My%20advice%20for%20your%20first%20day%20at%20the%20new%20job%20is%22%0A%0A%20%20%20%20_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20_s%20in%20SCALES%3A%0A%20%20%20%20%20%20%20%20_t1%20%3D%20gen(PROMPT_SPEECH%2C%20pirate_sv(_s))%0A%20%20%20%20%20%20%20%20_t2%20%3D%20gen(PROMPT_WEATHER%2C%20pirate_sv(_s))%0A%20%20%20%20%20%20%20%20_t3%20%3D%20gen(PROMPT_JOB%2C%20pirate_sv(_s))%0A%20%20%20%20%20%20%20%20_rows.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22scale%22%3A%20_s%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%F0%9F%8F%B4%E2%80%8D%E2%98%A0%EF%B8%8F%22%3A%20pirate_score(_t1)%20%2B%20pirate_score(_t2)%20%2B%20pirate_score(_t3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22speech%22%3A%20_t1.replace(%22%5Cn%22%2C%20%22%20%22)%5B%3A80%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22weather%22%3A%20_t2.replace(%22%5Cn%22%2C%20%22%20%22)%5B%3A80%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22job%20advice%20(generalization)%22%3A%20_t3.replace(%22%5Cn%22%2C%20%22%20%22)%5B%3A80%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20mo.ui.table(_rows%2C%20selection%3DNone)%0A%20%20%20%20return%20PROMPT_WEATHER%2C%20SCALES%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20A%20second%20metric%3A%20the%20distribution%20shift%0A%0A%20%20%20%20The%20%60%F0%9F%8F%B4%E2%80%8D%E2%98%A0%EF%B8%8F%60%20word%20count%20above%20already%20quantifies%20the%20*sampled%20generations*%20%E2%80%94%0A%20%20%20%20simple%20and%20direct%2C%20though%20lexicon-bound%20and%20noisy%20per%20completion.%20A%0A%20%20%20%20complementary%2C%20finer-grained%20measure%20looks%20at%20the%20**distribution**%20instead%3A%0A%20%20%20%20under%20each%20scale%20we%20score%20two%20fixed%20continuations%20of%20the%20same%20prefix%20with%0A%20%20%20%20%60echo%60%20%2B%20%60prompt_logprobs%60%20%E2%80%94%20one%20pirate-y%2C%20one%20plain%20%E2%80%94%20and%20compare%20**mean%0A%20%20%20%20logprob%20per%20token**.%20The%20shared%20prefix%20cancels%20in%20the%20difference%3A%0A%0A%20%20%20%20%3E%20preference%20%3D%20lp%2Ftok(pirate%20continuation)%20%E2%88%92%20lp%2Ftok(plain%20continuation)%0A%0A%20%20%20%20Read%20it%20by%20its%20**slope**%3A%20steering%20shifts%20the%20preference%20steadily%20toward%0A%20%20%20%20pirate.%20The%20zero%20crossing%20%E2%80%94%20the%20point%20where%20pirate-speak%20becomes%20outright%0A%20%20%20%20*more%20likely%20than%20plain%20English*%2C%20token%20for%20token%20%E2%80%94%20comes%20later%20(here%0A%20%20%20%20%E2%89%88%202.5)%20than%20the%20first%20visibly%20pirate%20samples%20(%E2%89%88%201.5%E2%80%932.0).%20That's%20expected%3A%0A%20%20%20%20sampling%20at%20temperature%20surfaces%20pirate%20tokens%20well%20before%20the%20average%0A%20%20%20%20likelihood%20favors%20an%20exaggerated%20dialect%20string.%20The%20two%20metrics%20agree%20on%0A%20%20%20%20direction%20and%20together%20bracket%20the%20effect.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(MODEL%2C%20PROMPT_WEATHER%2C%20SCALES%2C%20mo%2C%20pirate_sv%2C%20post)%3A%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%0A%20%20%20%20%23%20NB%3A%20continuations%20must%20START%20WITH%20A%20SPACE%20%E2%80%94%20that%20keeps%20the%20prefix's%20BPE%0A%20%20%20%20%23%20tokenization%20stable%20when%20we%20tokenize%20prefix%2Bcontinuation%20together%2C%20so%0A%20%20%20%20%23%20slicing%20by%20the%20bare%20prefix's%20token%20count%20is%20exact.%0A%20%20%20%20_CONT_PIRATE%20%3D%20%22%20arr%2C%20'tis%20be%20a%20fine%20day%20for%20sailin'%2C%20me%20hearties!%22%0A%20%20%20%20_CONT_PLAIN%20%3D%20%22%20it%20will%20be%20sunny%20with%20light%20winds%20in%20the%20afternoon.%22%0A%0A%20%20%20%20_n_prefix%20%3D%20post(%0A%20%20%20%20%20%20%20%20%7B%22model%22%3A%20MODEL%2C%20%22prompt%22%3A%20PROMPT_WEATHER%2C%20%22max_tokens%22%3A%201%2C%20%22temperature%22%3A%200%7D%0A%20%20%20%20)%5B%22usage%22%5D%5B%22prompt_tokens%22%5D%0A%0A%20%20%20%20def%20_lp_per_token(cont%3A%20str%2C%20sv%3A%20dict)%20-%3E%20float%3A%0A%20%20%20%20%20%20%20%20body%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22model%22%3A%20MODEL%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22prompt%22%3A%20PROMPT_WEATHER%20%2B%20cont%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22max_tokens%22%3A%201%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22temperature%22%3A%200%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22echo%22%3A%20True%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22prompt_logprobs%22%3A%200%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22apply_steering_vectors%22%3A%20%5Bsv%5D%2C%20%20%23%20sent%20at%20every%20strength%2C%200%20included%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20pl%20%3D%20post(body)%5B%22choices%22%5D%5B0%5D%5B%22prompt_logprobs%22%5D%0A%20%20%20%20%20%20%20%20xs%20%3D%20%5Blist(d.values())%5B0%5D%5B%22logprob%22%5D%20for%20d%20in%20pl%5B_n_prefix%3A%5D%20if%20d%5D%0A%20%20%20%20%20%20%20%20return%20sum(xs)%20%2F%20len(xs)%0A%0A%20%20%20%20_pref%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(s%2C%20_lp_per_token(_CONT_PIRATE%2C%20pirate_sv(s))%20-%20_lp_per_token(_CONT_PLAIN%2C%20pirate_sv(s)))%0A%20%20%20%20%20%20%20%20for%20s%20in%20SCALES%0A%20%20%20%20%5D%0A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(6.5%2C%203.2))%0A%20%20%20%20_ax.plot(*zip(*_pref)%2C%20marker%3D%22o%22%2C%20color%3D%22%237c3aed%22)%0A%20%20%20%20_ax.axhline(0%2C%20lw%3D1%2C%20color%3D%22gray%22%2C%20ls%3D%22%3A%22)%0A%20%20%20%20_ax.set_xlabel(%22steering%20scale%22)%0A%20%20%20%20_ax.set_ylabel(%22pirate%20%E2%88%92%20plain%20(lp%2Ftoken)%22)%0A%20%20%20%20_ax.set_title(%22Preference%20for%20pirate%20continuations%20vs.%20steering%20scale%22)%0A%20%20%20%20_ax.spines%5B%5B%22top%22%2C%20%22right%22%5D%5D.set_visible(False)%0A%20%20%20%20mo.vstack(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.ui.table(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5B%7B%22scale%22%3A%20s%2C%20%22preference%20(lp%2Ftoken)%22%3A%20round(p%2C%202)%7D%20for%20s%2C%20p%20in%20_pref%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20selection%3DNone%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20_fig%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20%E2%9A%A0%EF%B8%8F%20What%20did%20*not*%20work%3A%20instruction-pair%20contrast%20on%20a%20base%20model%0A%0A%20%20%20%20The%20classic%20%5Brepeng%2FCAA%5D(https%3A%2F%2Fvgel.me%2Fposts%2Frepresentation-engineering%2F)%0A%20%20%20%20recipe%20builds%20the%20direction%20from%20**paired%20instructions**%20%E2%80%94%20%60%22Talk%20like%20a%0A%20%20%20%20pirate.%22%60%20vs.%20%60%22Talk%20like%20a%20normal%20person.%22%60%20over%20shared%20neutral%20suffixes%2C%0A%20%20%20%20reading%20the%20residual%20at%20the%20last%20prompt%20token.%20It's%20standard%20and%20strong%20on%0A%20%20%20%20**instruct**%20models%2C%20where%20the%20instruction%20heavily%20conditions%20that%20state.%0A%0A%20%20%20%20We%20measured%20it%20here%20so%20you%20don't%20have%20to%3A%20on%20this%20**base**%20model%20it%20produced%0A%20%20%20%20only%20mild%20nautical%20drift%20with%20no%20persona%20shift%20(pirate-word%20score%20%E2%89%A45%2C%20vs.%20~19%0A%20%20%20%20for%20the%20text%20contrast%20above%20at%20its%20sweet%20spot%2C%20scale%202).%20A%20base%20model%20doesn't%20*obey*%0A%20%20%20%20the%20instruction%20%E2%80%94%20the%20prefix%20is%20merely%20weak%20document%20context.%20A%20document-framed%0A%20%20%20%20variant%20(%60%22The%20following%20is%20a%20transcript%20of%20a%20pirate%20captain%20speaking%3A%22%60)%0A%20%20%20%20failed%20differently%3A%20the%20%22transcript%20of%20someone%20speaking%22%20framing%20injects%20a%0A%20%20%20%20dominant%20first-person%20direction%2C%20and%20steering%20collapses%20into%20%60%22I%2C%20I%2C%20I%E2%80%A6%22%60.%0A%0A%20%20%20%20**Rule%20of%20thumb%3A%20on%20a%20base%20model%2C%20contrast%20actual%20text%20in%20the%20target%20style**%20%E2%80%94%0A%20%20%20%20the%20distribution%20you%20want%20to%20induce%20%E2%80%94%20rather%20than%20states%20downstream%20of%20an%0A%20%20%20%20instruction%20the%20model%20was%20never%20trained%20to%20follow.%0A%0A%20%20%20%20%23%23%20Takeaways%0A%0A%20%20%20%20-%20The%20whole%20loop%20%E2%80%94%20capture%2C%20decode%2C%20encode%2C%20steer%2C%20score%20%E2%80%94%20is%20plain%0A%20%20%20%20%20%20%60%2Fv1%2Fcompletions%60%20calls.%20No%20GPU%2C%20no%20model%20surgery.%0A%20%20%20%20-%20**Always%20keep%20a%20%60scale%3A%200%60%20identity%20check**%20in%20your%20harness%2C%20compared%0A%20%20%20%20%20%20same-engine%20(activation%20requests%20run%20on%20their%20own%20engine).%0A%20%20%20%20-%20**Calibrate%20strength%20from%20small**%20and%20watch%20for%20repetition%2Fdegeneration%20%E2%80%94%0A%20%20%20%20%20%20the%20useful%20window%20here%20was%20scale%20%E2%89%881.5%E2%80%932.0%3B%20by%203%20the%20output%20collapses.%0A%20%20%20%20-%20Practicalities%3A%20activation-capable%20models%20advertise%20%60%22activations%22%3A%20true%60%20in%0A%20%20%20%20%20%20%60GET%20%2Fv1%2Fmodels%60%3B%20capture%20prompts%20are%20length-capped%20per%20model%3B%20the%20engine%0A%20%20%20%20%20%20cold-boots%20after%20idle%3B%20activation%20requests%20count%20against%20a%20per-key%20quota.%0A%0A%20%20%20%20**Docs%3A**%20%5Bactivation%20harvesting%5D(https%3A%2F%2Finfra.acsresearch.org%2Ftutorial%2Factivation-harvesting)%20%C2%B7%0A%20%20%20%20%5Bactivation%20steering%5D(https%3A%2F%2Finfra.acsresearch.org%2Ftutorial%2Factivation-steering)%20%C2%B7%0A%20%20%20%20%5BAPI%20reference%5D(https%3A%2F%2Finfra.acsresearch.org%2Ftutorial%2Fapi)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
a3ea0f2cf5447e43e78bd8529e15a2bc